8

If I have this in my controller

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

I can only access the data in $array inside the home.index.blade.php template file, and NOT in the layouts.main that I am using.

The layout & the view load correctly, I just cannot access any data passed in a controller from the layout file.

Anyone out there that can help?

To Clarify - Say I wanted to pass a title variable to my layout.main, how would I go about it? the method I posted above only allows me to access $array from within the 'content' of index.blade.php

2 Answers 2

9

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.