Laravel check if collection is empty
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
How can I check if collection empty in view blade laravel?
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
How to check if a Laravel Collection is empty?
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
How to not show empty collection
Hello if $user_wallet contains the collection data then you need to change the @if
condition.
Replace your
@if(!empty($user_wallet))
To
@if(!$user_wallet->isEmpty()) OR @if($user_wallet->isNotEmpty())
Eloquent Collection: Counting and Detect Empty
When using ->get()
you cannot simply use any of the below:
if (empty($result)) { }
if (!$result) { }
if ($result) { }
Because if you dd($result);
you'll notice an instance of Illuminate\Support\Collection
is always returned, even when there are no results. Essentially what you're checking is $a = new stdClass; if ($a) { ... }
which will always return true.
To determine if there are any results you can do any of the following:
if ($result->first()) { }
if (!$result->isEmpty()) { }
if ($result->count()) { }
if (count($result)) { }
You could also use ->first()
instead of ->get()
on the query builder which will return an instance of the first found model, or null
otherwise. This is useful if you need or are expecting only one result from the database.
$result = Model::where(...)->first();
if ($result) { ... }
Notes / References
->first()
http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_firstisEmpty()
http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_isEmpty->count()
http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_countcount($result)
works because the Collection implements Countable and an internalcount()
method: http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Collection.html#method_count
Bonus Information
The Collection and the Query Builder differences can be a bit confusing to newcomers of Laravel because the method names are often the same between the two. For that reason it can be confusing to know what one you’re working on. The Query Builder essentially builds a query until you call a method where it will execute the query and hit the database (e.g. when you call certain methods like ->all()
->first()
->lists()
and others). Those methods also exist on the Collection
object, which can get returned from the Query Builder if there are multiple results. If you're not sure what class you're actually working with, try doing var_dump(User::all())
and experimenting to see what classes it's actually returning (with help of get_class(...)
). I highly recommend you check out the source code for the Collection class, it's pretty simple. Then check out the Query Builder and see the similarities in function names and find out when it actually hits the database.
- Laravel 5.2 Collection Class
- Laravel 5.2 Query Builder
Laravel Eloquent: Detect is empty or count
#1
if($user->delete())
return true;
else
return false;
if($user->save()){
return true;
}
#2
To determine if there are any results you can do any of the following:
if (!$user->isEmpty()) { }
if ($user->count()) { }
if (count($user)) { }
Notes / References
http://laravel.com/api/5.6/Illuminate/Database/Eloquent/Collection.html#method_first
http://laravel.com/api/5.6/Illuminate/Database/Eloquent/Collection.html#method_isEmpty
http://laravel.com/api/5.6/Illuminate/Database/Eloquent/Collection.html#method_count
http://laravel.com/api/5.6/Illuminate/Database/Eloquent/Collection.html#method_count
Related Topics
Why Preg_Replace Throws Me a "Unknown Modifier" Error
PHP Timezone Database Is Corrupt Error
Get Content Within a HTML Tag Using PHP and Replace It After Processing
How to Add Two Strings Together
Fatal Error: Call to Undefined Function Openssl_Random_Pseudo_Bytes()
Getting the Text Portion of a Node Using PHP Simple Xml
PHP Script to Execute at Certain Times
How to Make Sure That Values from MySQL Keep Their Type in PHP
PHP Optional Parameters - Specify Parameter Value by Name
File Download Script Doesn't Work When Called from Ajax