Reputation: 5630
I am attempting to loop out a menu in Laravel.
I am passing the nested array to the main blade template categories-management.blade.php
.
View::make('categories-management')->with('categories', $categories);
Where $categories
is
array (size=3)
'Business Resources' =>
array (size=7)
'Operations' =>
array (size=4)
'resource_type_id' => int 1
27 =>
array (size=3)
'id' => int 27
'name' => string 'Design & Development' (length=20)
'children' =>
array (size=2)
... truncated ...
I am then kicking of the menu inside of categories-management.blade.php
with this:
@include('/includes/category-menu-item', array('categories', $categories))
Inside includes/category-menu-item
I have the following loop:
@if(is_array($categories))
<ul>
@foreach($categories as $key => $value)
<li>
@if(!is_numeric($key))
<p>{{$key}}</p>
@include('/admin/includes/category-menu-item', array('categories', $value))
@else
<button data-category-id="{{$value->id}}">{{$value->name}}</button>
@include('/admin/includes/category-menu-item', array('categories', $value->children))
@endif
</li>
@endforeach
</ul>
@endif
This is still obviously incomplete but I already have issues where the template never gets past the first layer and prints out:
Business Resources
Business Resources
Business Resources
Business Resources
... etc ...How do I get Laravel to recognise the new array value? Or is there a better method for this?
Upvotes: 0
Views: 1076
Reputation: 8415
Your @include
statements are not correct. The data passed to view need to be an associated array, you have passed an array with numeric keys. You have to change from array('categories', $value)
into array('categories' => $value)
:
@if(is_array($categories))
<ul>
@foreach($categories as $key => $value)
<li>
@if(!is_numeric($key))
<p>{{$key}}</p>
@include('/admin/includes/category-menu-item', array('categories' => $value))
@else
<button data-category-id="{{$value->id}}">{{$value->name}}</button>
@include('/admin/includes/category-menu-item', array('categories' => $value->children))
@endif
</li>
@endforeach
</ul>
@endif
Upvotes: 1