I am trying to pass a variable to my basic layout. This is because I need it in all pages. I thought that writing something like that on my BaseController
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$footertext = MyText::where('status', 1);
$this->layout = View::make($this->layout, ['footertext' =>$footertext ]);
}
}
}
And on my I thought that writing something like that on my main.blade.php could work.
{{ $footertext }}.
Instead I having this error,
Undefined variable: footertext
and after two hours of looking around...I didn't find any solution. Any help is welcome.
Not long ago I was trying to do the same.
If you are using Laravel 5 you can edit the AppServiceProvider.php inside app/Providers
and register a provider for this layout like:
public function boot()
{
view()->composer('my.layout', function($view) {
$myvar = 'test';
$view->with('data', array('myvar' => $myvar));
});
}
Now if you are using Laravel 4 I think it's more simple. In the app/filters.php
:
View::composer('my.layout', function ($view) {
$view->with('variable', $variable);
});
In both ways any variable you pass will be available to all templates that are extending the master template.
References:
https://laracasts.com/discuss/channels/general-discussion/laravel-5-pass-variables-to-master-template https://coderwall.com/p/kqxdug/share-a-variable-across-views-in-laravel?p=1&q=author%3Aeuantor
For laravel 5.3 I am using in AppServiceProvider.php
inside app/Providers
public function boot()
{
view()->composer('layouts.master', function($view)
{
$view->with('variable', 'myvariable');
});
}
Reference: https://laracasts.com/discuss/channels/general-discussion/laravel-5-pass-variables-to-master-template
*Dedicated Class inlcuded
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With