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?
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');
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