This is probably something simple, but it's doing my head in.
So, my layout blade template has this:
@include('layouts.partials.sidebar')
{{ $slot }}
@include('layouts.partials.footer')
@include('layouts.partials.scripts')
I create a view which loads a template. This I assume gets parsed in $slot.
return view('request', [
'boo' => 'Hoo'
]);
No problems, the page loads and the variable 'boo' is accessible as {{ $boo }} in the 'requests' template.
But my question is, how can I pass the 'boo' variable to an included file in the layout file? In this case the following:
@include('layouts.partials.scripts')
So, in 'layouts.partials.scripts' how can I access {{ $boo }}? At the moment I just get an undefined index error.
Thank you very much for the help.
@include('layouts.partials.scripts', ['boo' => 'Hoo'])
Laravel docs: https://laravel.com/docs/8.x/blade#including-subviews
If you have a partial like a nav, header or sidebar which is part of the master layout from which you are composing other views, and it requires some data which doesn't change from one view to another, like navigation links, then, instead of passing the data from each controller method, you can define a view composer in a service provider's boot() method:
//Service Provider's boot method
public function boot()
{
View::composer('layouts.partials.sidebar', function ($view) {
//$links = get the data for links
return $view->with('links', $links);
});
}
Laravel docs: https://laravel.com/docs/master/views#view-composers