简体   繁体   中英

Passing data from controller to laravel nested view

I have a page system in Laravel - where I pass data from controller to view.

$this->data['title'] = $row->title;
$this->data['breadcrumb'] = $row->bc;

Now I passed it as follows:

return View::make('Themes.Page', $this->data);

In the view file, I access the data as follows:

{{$breadcrumb}}

What I am trying to do now is to pass this data in nested views:

$this->layout->nest('content',$page, $this->data);

(Content is the {{content}} in the view which will be replaced with $page contents. I want to pass the $this->data just as before but now I get an error:

Variable breadcrumb not defined.

Note: Laravel Version 4.2 $this->layout is set in constructor to a template file (Themes.Page)

Actually you don't need to pass any separate data to your partial page(breadcrumb)

controller page

$this->data['title'] = $row->title;
$this->data['breadcrumb'] = $row->bc;

return View::make('idea.show',array("data"=>$this->data));

main view page

<div>
<h1>here you can print data passed from controller  {{$data['title']}}</h1>
@include('partials.breadcrumb')
</div>

your partial file

<div>
<h1>here also you can print data passed from controller {{$data['title']}}</h1>
<ul>
<li>....<li>
<li>....<li>
</ul>
</div>

for more information on this you can check following links http://laravel-recipes.com/recipes/90/including-a-blade-template-within-another-template or watch this videohttps://laracasts.com/series/laravel-5-fundamentals/episodes/13

You should pass data as follows

return View::make('Themes.Page')->with(array(
            'data'=>$this->data));

or (since you're passing only 1 variable)

return View::make('Themes.Page')->with('data', $this->data);

and further you can pass it on to nested views as by referencing $data

$dataForNestedView = ['breadcrumb' => $row->bc];

return View::make('Themes.Page', $this->data)->nest('content', 'page.content', $dataForNestedView);

In the Themes.Page view render nested view:

<div>
   {{ $content }} <!-- There will be nested view -->
</div> 

And in the nested page.content view you can call:

<div>
  {{ $breadcrumb }}
</div>

*div tag is only for better understanding.

Okay, after intense search, I figured out that there was a bug in the Laravel version 4.2 I was using. Laravel 5 works. For laravel 4.2, a better option would be to pass array of data objects using View::share('data',$objectarray) while passing the data from controller.

Thanks everyone for help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM