S M Abrar Jahin
S M Abrar Jahin

Reputation: 14588

Showing Nested Data in Laravel Blade

I have a nested data like this which is passed to blade-

enter image description here

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-

enter image description here

Can anyone please help?

Thanks in advance for helping.

Upvotes: 1

Views: 985

Answers (2)

Achraf Khouadja
Achraf Khouadja

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

Parvez Rahaman
Parvez Rahaman

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

Related Questions