protonemedia / laravel-splade

💫 The magic of Inertia.js with the simplicity of Blade 💫 - Splade provides a super easy way to build Single Page Applications (SPA) using standard Laravel Blade templates, and sparkle it to make it interactive. All without ever leaving Blade.
https://splade.dev
MIT License
1.47k stars 112 forks source link

PersistenLayout Not Working #544

Closed kahfieidn closed 11 months ago

kahfieidn commented 11 months ago

Follow the instructions given in the documentation. https://splade.dev/docs/persistent-layout

I try to implement persistent layout but not working

here is the error :

ProtoneMedia\Splade\Components\PersistentComponent::viewData(): Argument #2 ($slot) must be of type Illuminate\Support\HtmlString, Illuminate\View\ComponentSlot given, 

Any solve for this?

kahfieidn commented 11 months ago

I solved this with while solution:

go to dir : vendor/protonemedia/laravel-splade/src/Components/PersistentComponent.php

public function viewData(array $originalData, HtmlString $slot, Factory $env): array
    {
        $slots = Collection::make($env->getFirstSlot())->map(function (ComponentSlot $slot, $name) {
            return new ComponentSlot(
                $this->wrapSlotContents($name, $slot)->toHtml(),
                $slot->attributes->getAttributes()
            );
        });

        return array_merge(
            $originalData,
            $slots->all(),
            ['slot' => $this->wrapSlotContents('slot', $slot)]
        );
    }

Remove HtmlString in behind $slot, then finaly function viewData like this:

public function viewData(array $originalData, $slot, Factory $env): array
    {
        $slots = Collection::make($env->getFirstSlot())->map(function (ComponentSlot $slot, $name) {
            return new ComponentSlot(
                $this->wrapSlotContents($name, $slot)->toHtml(),
                $slot->attributes->getAttributes()
            );
        });

        return array_merge(
            $originalData,
            $slots->all(),
            ['slot' => $this->wrapSlotContents('slot', $slot)]
        );
    }

Working fine for me, this is not best solution, maybe anyone have better solution

Louxsdon commented 11 months ago

Replace HtmlString with ComponentSlot instead of removing it. After that the viewData should look like below

public function viewData(array $originalData, ComponentSlot $slot, Factory $env): array
    {
        $slots = Collection::make($env->getFirstSlot())->map(function (ComponentSlot $slot, $name) {
            return new ComponentSlot(
                $this->wrapSlotContents($name, $slot)->toHtml(),
                $slot->attributes->getAttributes()
            );
        });

        return array_merge(
            $originalData,
            $slots->all(),
            ['slot' => $this->wrapSlotContents('slot', $slot)]
        );
    }
kahfieidn commented 11 months ago

Thanks a lot! Working fine