Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Laravel: how to pass a variable to my basic layout

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.

like image 204
Red Cube Avatar asked Mar 16 '23 18:03

Red Cube

2 Answers

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.


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

like image 152
Thiago Avatar answered Mar 18 '23 06:03


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

like image 32
Grig Dodon Avatar answered Mar 18 '23 07:03

Grig Dodon