Reputation: 14588
I have a nested data like this which is passed to blade-
I want to display it's data in blade view.
So what I have done is-
<ul class="dropdown-menu h-ctr h-ctr2 col-md-12 col-sm-12">
@foreach ($categories as $category)
<li class="no-border">
<label class="pull-left">
<input type="checkbox" value="{{ $category->id }}" checked>
<strong> {{ $category->name }} (21)</strong>
</label>
<ul>
@foreach($category->sub_category as $sub_cat)
<li>
<label class="pull-left">
<input type="checkbox" checked value="1"> {{ $sub_cat->name }} (7)
</label>
</li>
@endforeach
</ul>
</li>
@endforeach
</ul>
And I am getting error for nested loop's part-
foreach($category->sub_category as $sub_cat)
<li>
<label class="pull-left">
<input type="checkbox" checked value="1"> {{ $sub_cat->name }} (7)
</label>
</li>
@endforeach
The error is like this-
Can anyone please help?
Thanks in advance for helping.
Upvotes: 1
Views: 985
Reputation: 6279
Try this mate
@foreach($category['sub_category'] as $sub_cat)
<li>
<label class="pull-left">
<input type="checkbox" checked value="1"> {{ $sub_cat->name }} (7)
</label>
</li>
@endforeach
in case this didnt work can you share you controller code too?
EDIT : in your controller try to convert the array to a collection (there are simpler why like using eloquent
$collection = collect($myarray);
Upvotes: 1
Reputation: 4397
Try this..
@if($category->sub_category)
@foreach($category->sub_category as $sub_cat)
<li>
<label class="pull-left">
<input type="checkbox" checked value="1"> {{ $sub_cat->name }} (7)
</label>
</li>
@endforeach
@endif
Upvotes: 0