Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Check Empty Array

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.

like image 339
Mamulasa Avatar asked Sep 06 '16 10:09

Mamulasa


People also ask

How do you check if an array is empty or not?

To check if an array is empty or not, you can use the .length property. The length property sets or returns the number of elements in an array. By knowing the number of elements in the array, you can tell if it is empty or not. An empty array will have 0 elements inside of it.

How do you check if a variable is empty in laravel?

How do you know if a variable is empty in laravel? The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.


1 Answers

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

@if($daily->articles->isEmpty())
like image 68
Alexey Mezenin Avatar answered Sep 22 '22 19:09

Alexey Mezenin