Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - How to pass data to include

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?

like image 468
Codearts Avatar asked May 06 '16 15:05

Codearts


People also ask

How pass data from model to controller in Laravel?

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).

How can pass multiple values from controller view in Laravel?

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.


2 Answers

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'])
like image 53
Alexey Mezenin Avatar answered Oct 05 '22 23:10

Alexey Mezenin


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');
});
like image 40
Jeff Lambert Avatar answered Oct 05 '22 23:10

Jeff Lambert