6

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.

1 Answer 1

19

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

@if($daily->articles->isEmpty())
Sign up to request clarification or add additional context in comments.

1 Comment

GREAT! THANK YOU Alexey, you helped me out again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.