Laravel check if collection is empty

PhpLaravelLaravel 5.2

Php Problem Overview


I've got this in my Laravel webapp:

@foreach($mentors as $mentor)
	@foreach($mentor->intern as $intern)
		<tr class="table-row-link" data-href="/werknemer/{!! $intern->employee->EmployeeId !!}">
			<td>{{ $intern->employee->FirstName }}</td>
			<td>{{  $intern->employee->LastName }}</td>
		</tr>
	@endforeach
@endforeach

How could I check if there are any $mentors->intern->employee ?

When I do :

@if(count($mentors))

It does not check for that.

Php Solutions


Solution 1 - Php

To determine if there are any results you can do any of the following:

if ($mentor->first()) { } 
if (!$mentor->isEmpty()) { }
if ($mentor->count()) { }
if (count($mentor)) { }
if ($mentor->isNotEmpty()) { }

Notes / References

->first()

https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Collection.html#method_first

isEmpty() https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Collection.html#method_isEmpty

->count()

https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Collection.html#method_count

count($mentors) works because the Collection implements Countable and an internal count() method:

https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Collection.html#method_count

isNotEmpty()

https://laravel.com/docs/5.7/collections#method-isnotempty

So what you can do is :

if (!$mentors->intern->employee->isEmpty()) { }

Solution 2 - Php

You can always count the collection. For example $mentor->intern->count() will return how many intern does a mentor have.

https://laravel.com/docs/5.2/collections#method-count

In your code you can do something like this

foreach($mentors as $mentor)
    @if($mentor->intern->count() > 0)
    @foreach($mentor->intern as $intern)
        <tr class="table-row-link" data-href="/werknemer/{!! $intern->employee->EmployeeId !!}">
            <td>{{ $intern->employee->FirstName }}</td>
            <td>{{  $intern->employee->LastName }}</td>
        </tr>
    @endforeach
    @else
        Mentor don't have any intern
    @endif
@endforeach

Solution 3 - Php

Starting from Laravel 5.3 you can simply use :

if ($mentor->isNotEmpty()) {
//do something.
}

Documentation https://laravel.com/docs/5.5/collections#method-isnotempty

Solution 4 - Php

This is the fastest way:

if ($coll->isEmpty()) {...}

Other solutions like count do a bit more than you need which costs slightly more time.

Plus, the isEmpty() name quite precisely describes what you want to check there so your code will be more readable.

Solution 5 - Php

This is the best solution I found so far.

in blade

@if($mentors->count() == 0)
    <td colspan="5" class="text-center">
        Nothing Found
    </td>
@endif

in controller

if ($mentors->count() == 0) {
    return "Nothing Found";
}

Solution 6 - Php

From php7 you can use Null Coalesce Opperator:

$employee = $mentors->intern ?? $mentors->intern->employee

This will return Null or the employee.

Solution 7 - Php

I prefer

(!$mentor)

Is more effective and accurate

Solution 8 - Php

First you can convert your collection to an array. Next run an empty method like this:

if(empty($collect->toArray())){}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJamieView Question on Stackoverflow
Solution 1 - PhpDrudge RajenView Answer on Stackoverflow
Solution 2 - PhpotezzView Answer on Stackoverflow
Solution 3 - PhpMohamed Salem LamiriView Answer on Stackoverflow
Solution 4 - PhpCsongor HalmaiView Answer on Stackoverflow
Solution 5 - PhpKesara WimalView Answer on Stackoverflow
Solution 6 - Phpd.raevView Answer on Stackoverflow
Solution 7 - PhpMarcello PattoView Answer on Stackoverflow
Solution 8 - Phpamirreza sharifiView Answer on Stackoverflow