0

I've recently started working with Laravel during one of my courses and those components look really promising. One problem though, that I've come across, is passing data from, let's say, the controller, to a nested component.

Here's my (oversimplified) setup:

mainframe.blade.php (used as my layout template) :

<body>
    <!-- Navigation-->
    <x-main-menu></x-main-menu>
        
    <!-- Page Header-->
    <x-master-header :headerText="$headerText"></x-master-header>

    {{ $content }}
</body>

master-header.blade.php :

<header>
    <h1>{{ $headerText }}</h1>
</header>

and finally, home.blade.php, that "extends" my layout page :

<x-mainframe>
    <x-slot name="content">
        *... some content*
    </x-slot>
</x-mainframe>

Now, I thought that it would as trivial as simply passing the $headerText content through my controller, like so :

public function home() {
    return view("home", [
        "headerText" => "Je teste les variables des templates Blade"
    ]);
}

... but apparently, that variable is passed only to the initial "home" view, but not all the way through my header component as I'm getting an error.

I've been reading on View Composer as a solution, but I thought there might be a simpler way of doing that.

So, as asked in the title, how can I pass data from a grand parent component to one of his grand children component ?

Thanks !

2
  • You have to pass $headerText to mainframe <x-mainframe : headerText="$headerText"... Or use a slot inside your mainframe.blade.php in order to define the contents of x-header from your home.blade.php Commented Aug 19, 2021 at 13:04
  • @NICO Yeah, tried that earlier but still throws an undefined variable error in mainframe.blade.php ;\ Commented Aug 19, 2021 at 13:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.