How to check if a Laravel Collection is empty?
Asked Answered
C

8

21

I create a view by doing an eloquent query and then pass it over to Blade.

@if($contacts != null)
//display contacts
@else
You dont have contacts
@endif

However it always assume that $contacts has something even if the query gives me nothing.

I did dd($contacts) and get:

Collection {#247 ▼
  #items: []
}

How do I check if it is empty?

Calisaya answered 21/12, 2016 at 18:8 Comment(0)
I
57

If it is a Eloquent Collection as it appears to be from your example you can use the isEmpty collection helper function;

@if(!$contacts->isEmpty())
//display contacts
@else
You dont have contacts
@endif

Collections Documentation

Ite answered 21/12, 2016 at 18:10 Comment(0)
A
22

There are few ways:

if (!empty($contacts))

if (!contacts->isEmpty())

if (count($contacts) > 0)

if ($contacts->count() > 0)
Alston answered 21/12, 2016 at 18:22 Comment(2)
You'll run into issues with count($contacts) > 0 on PHP 7.2+Babbittry
if (!empty($contacts)) - This is not giving correct outputSpoils
S
5

Your Eloquent query returns an array of result, so you can use count.

@if(count($contacts) > 0)
//Display contacts
@else
//No contacts
@endif
Stony answered 21/12, 2016 at 18:12 Comment(3)
It's not an array of results, it's an Eloquent Collection object that implements array interfaces.Cosec
@JeremyHarris of course you're right, I just meant the result is an array :)Stony
this is what I needed. I was trying to check whether my url parameters were empty, using empty($request->request), but that was returning false even when no query strings were set. Your method worked however.Meister
E
5

Your $contacts is empty. Bcoz Your query is unable to get data. Once your query unable to get data it's return an empty arrya. So check it

    @if($contacts->isEmpty())
    {{ 'Empty' }} 
    @else
   {{ 'you have data' }}
    @endif
Enfold answered 21/12, 2016 at 18:14 Comment(0)
W
4

You can use blank($contacts)
Helpers Laravel: blank

Wrangler answered 15/4, 2018 at 7:31 Comment(0)
F
2

You must do the following:

in the view of your " ContactController ":

public function index()
{
   $contacts = Contact::all();
   return view('page.index', compact('contacts'))
}

later on view " index.blade.php ":

@if (collect($contacts)->isEmpty()) {{-- remember that $contact is your variable --}}
   <p>There is no record available at this time</p>
@else
   @foreach($contacts as $contact)
      {{$contact->name_contact}}
   @endforeach
@endif

Now, I suppose you have a model called Contact

Figurative answered 8/1, 2020 at 23:56 Comment(0)
S
1
if(count($profiles) > 0){
            return redirect()->action('NameController@name');
        }else{
            return view('user');
        }

this also works fine in a controller

Supersonic answered 13/12, 2018 at 11:30 Comment(0)
S
1

i added method as global in my helper class

function isNullOrEmpty($value)
{
    return is_null($value) || empty($value);
}
Sousa answered 16/8, 2020 at 11:37 Comment(1)
return is_null($value) || empty($value); is working too hard -- this is equivalent to return !$value.Shoon

© 2022 - 2024 — McMap. All rights reserved.