So essentially all of my views are using the header.blade.php since I am including it in my master layout. I need to pass data to the header on every single view. Is there a way to pass data just to the include rather than passing the data for the header in each view?
You can pass values or parameters from controller to model in laravel by creating a model instance and calling the model method by using $object->methodName($parameters).
Just pass it as an array: $data = [ 'name' => 'Raphael', 'age' => 22, 'email' => '[email protected]' ]; return View::make('user')->with($data); Or chain them, like @Antonio mentioned. The recommended way is to use compact method to pass variables.
You don't need to do that, but you can:
All variables that are available to the parent view will be made available to the included view. Even though the included view will inherit all data available in the parent view, you may also pass an array of extra data to the included view:
@include('view.name', ['some' => 'data'])
One option if you're trying to send data only to the included view is to use a view composer. They will fire even in the case of trying to prepare a view for @include
view()->composer('header', function($view) {
$view->with('data', 'some data');
});
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