How to check if a Laravel Collection is empty?
In many Laravel applications, you will frequently encounter scenarios where an Eloquent query returns a collection that might be empty. A common pattern involves passing this data directly to a Blade view and attempting to conditionally render content based on its presence.
Consider the following scenario: you have defined a route that queries for contacts and passes the result to a view. Inside the template, you might write:
@if($contacts != null)
<!-- Display contacts -->
@else
You don't have contacts
@endif
At first glance, this logic seems sound. The developer assumes that if $contacts is not null, it must contain data. However, in practice, this condition often evaluates to true even when the query returns zero results. This happens because an empty collection is a valid object instance; it is not null. When you inspect this variable using Laravel's debugging tools, you see:
Collection {#247 â¼
#items: []
}
The #items property is an empty array, but the object itself exists. The strict comparison against null fails to account for the state of the data contained within that instance. To correctly determine if there are any records available, you need to check the number of items rather than the object's existence.
The most direct and idiomatic way to solve this in Laravel is using the count() method or checking the count directly. Since a Collection wraps an array, calling count() on it returns an integer representing the number of elements. You can modify your Blade directive as follows:
@if($contacts->count() > 0)
<!-- Display contacts -->
@else
You don't have contacts
@endif
Alternatively, you can use the isEmpty() method, which is a specific helper designed for collections and arrays. This approach makes the intent of your code immediately obvious to anyone reading the template:
@if($contacts->isEmpty())
You don't have contacts
@else
<!-- Display contacts -->
@endif
Both approaches address the root cause identified in the source material. The issue was not that the variable was missing; it was that the developer was checking for object identity (null) instead of data presence. By shifting the focus to the content count, you align your logic with how Eloquent and Collections actually behave.
When building views that depend on database results, it is crucial to remember that an empty result set from a query still resolves to a valid instance of Illuminate\Support\Collection. This object persists even when the underlying dataset is empty. Therefore, any conditional logic must account for this reality. If you rely on truthiness checks like if ($contacts), an empty collection will actually evaluate to true because it is a populated object, regardless of whether its items array has content.
For developers working with complex data structures or those who prefer functional programming styles within Laravel, the isEmpty() method provides a clean abstraction over checking the count. It avoids the need to explicitly write a comparison operator and keeps the template logic concise. However, using count() > 0 is equally valid and sometimes preferred if you plan to iterate immediately after the check, as it gives you the numeric value directly for further calculations.
Understanding this distinction prevents subtle bugs where UI elements fail to hide or show when they should. Whether you are debugging a view that incorrectly displays "You don't have contacts" or trying to optimize a route that serves dynamic content, recognizing that null checks are insufficient for collections is a fundamental concept in Laravel development.
