How to check if a Laravel Collection is empty?
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Efficiently Checking if a Laravel Collection is Empty: A Comprehensive Guide
Body: In this blog post, we will explore different approaches to efficiently check if a Laravel collection is empty. When working with Laravel collections, it's essential to know how to validate their contents as it helps with better code structure and understanding your application's data flow. Let's dive into the methods you can utilize to achieve this goal:
1. Conditional Statement Checking: This approach is a straightforward way of validating if a collection has any items or not. You can use the Laravel collection's `count()` method, combined with an if-else statement or conditional ternary operator. Here's how it looks in code:
@if($contacts->count() > 0)
//display contacts
@else
You dont have contacts
@endif
However, this approach can become verbose when dealing with multiple collections. To simplify your code, it's recommended to use a different method.
2. Laravel Collection Methods: Laravel provides various methods that help you check whether a collection is empty or not, making the code more concise and less redundant. Here are some of them: - `isEmpty()`: Returns true if the collection has zero items.if ($contacts->isEmpty()) {
//display no contacts found message
} else {
//display contacts
}
- `isNotEmpty()`: The opposite of isEmpty(), returning false if the collection has zero items. This can be useful when you expect a non-empty array but don't want to explicitly check for it.
if (!$contacts->isNotEmpty()) {
//display no contacts found message
} else {
//display contacts
}
- `isEmpty()` vs. `count() > 0`: It's essential to understand the difference between these two methods. The isEmpty() method checks if the collection has zero items, whereas count() returns the number of items in the collection and then compares it to a value (in this case 0). Both approaches are valid, but isEmpty() may provide more clarity when reading your code.
if ($contacts->isEmpty()) {
//display no contacts found message
} else {
//display contacts
}
3. Enhancing the Response with Custom Messages: To ensure your Laravel application provides a better user experience, consider adding custom messages when displaying empty collections. This can be easily achieved using the Laravel Blade template engine. Here's an example:
@if($contacts->isEmpty())
You dont have contacts
@else
//display contacts
@endif