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.
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 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.
Because it's Laravel collection, you can use isEmpty()
helper:
@if($daily->articles->isEmpty())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With