Data passed to view, not accessible in layout - Laravel Controllers

删除回忆录丶 提交于 2019-12-04 07:33:18

To pass data to the layout you use the first parameter as the name of the variable.

What is happening when you are doing this:

$this->layout->nest('content', 'home.index', $array);

Is nesting the view home.index that takes a parameter of $array. You are not creating a variable that can be used in the layout.

The variable you are creating for the layout is $content that will display the contents of the view passed to it, this is specific to the 'nest' method.

If you look into the API you will notice that 'layout' as declared in the controller is an instance of: Laravel\View

This has a method called with that supplies variables to the view.

To use it try something like this:

$this->layout->with( 'myVariable' , $variable_to_pass_to_layout )
             ->nest('content', 'home.index', $array)

Another solution is to make the data available across all your views with

View::share('myVariable', $variable_to_pass_to_layout);

Might be useful to let anyone access it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!