10

If I dd($items); in the controller, the result like this :

enter image description here

In the view blade laravel I check if collection empty like this :

@if($items)
...
@endif

But it does not work

How can I solve this problem?

moses toh
  • 12,344
  • 71
  • 243
  • 443
  • Possible duplicate of [Need to check if an object is empty in laravel](https://stackoverflow.com/questions/41269198/need-to-check-if-an-object-is-empty-in-laravel) – Paresh Barad Aug 02 '18 at 12:31

3 Answers3

22

You could use $items->isEmpty(); or $items->isNotEmpty();

Like so:

@if(!$items->isNotEmpty())
...
@endif

You can further read over here: https://laravel.com/docs/5.5/collections#method-isempty

MyLibary
  • 1,763
  • 10
  • 17
  • Your if statement says when not not empty.. Just use: @if($items->isNotEmpty()) -> when not empty or @if($items->isEmpty()) -> when empty – Whitespacecode Sep 20 '22 at 07:45
0
@if ( $items->count() )
....
@endif
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
0

i normally double check so that if i don't get the value the page keeps running :

@if(isset($items))
@if(!empty($items))
.....
@endif
@endif


hope it helps