Laravel Check Empty Array

Mamulasa picture Mamulasa · Sep 6, 2016 · Viewed 16.8k times · Source

I still have trouble with checking if an array is empty or not in laravel.

This is my view:

@foreach($restaurantmenue as $daily)

                    @if(empty($daily->articles))
                      no article
                      @else
                        @foreach($daily->articles as $menue)
                            <a class="card-link" href="#">
                                <h4 class="title">{{$menue->title}} </h4>
                            </a>
                       @endforeach
                    @endif


                @endforeach

{{dd($daily->articles)}} When I check my views (One with an Article and the other without an article) I get this output:

The View with an existing article shows: Collection {#228 ▼ #items: array:1 [▶] }

And the view without an article shows: Collection {#227 ▼ #items: [] }

I have no idea why the code in the IF STATEMENT is not executed. The "No Article" Message is not displayed.

Answer

Alexey Mezenin picture Alexey Mezenin · Sep 6, 2016

Because it's Laravel collection, you can use isEmpty() helper:

@if($daily->articles->isEmpty())