Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5: How can I pass a variable from a controller method to app.blade.php?

I'm showing a variable in my layout file app.blade.php. That variable has a default value defined in my global 'view composer'. I need to overwrite that value from within a controller method, how can I do that?

like image 333
Agu Dondo Avatar asked Oct 31 '22 10:10

Agu Dondo


1 Answers

That's a tricky one. The problem is that the view composer gets trigger after the controller has returned the view. However you can check inside the view composer if the value has been set:

public function compose(View $view)
{
    if(!$view->offsetExists('foo')){
        $view->with('foo', 'default');
    }
}

And when you want to "override" the default in your controller:

return view('view.name')->with('foo', 'bar');
like image 197
lukasgeiter Avatar answered Nov 08 '22 06:11

lukasgeiter