Introduction
As a Laravel web developer, you'll often to be interacting with Collections, PHP arrays, and JavaScript arrays. A common task I come across regularly is to check whether these structures are empty or not.
If you're anything like me, you'll have likely tried interacting with the Collection class almost as if it was an array (whether this be accidentally or on purpose). Although you can generally do this, there is a common gotcha that you need to be aware of that can cause bugs in your code! In fact, I wrote this article because I was caught out by this gotcha myself and spent an embarrassing amount of time trying to figure out what was going on.
In this Quickfire post we're going to take look at some of the most common ways to check if a Collection is empty.
I've also published two other articles that explain how to check if a PHP array is empty and how to check if a JavaScript array is empty. I published them as a quick point of reference for myself that I can refer back to when my memory fails me about which method to use.
1. Using the isEmpty()
method
The fastest and most obvious method to use is the isEmpty
method that's available directly on the Laravel Collection class. It does exactly what it says on the tin and returns a boolean value indicating whether the Collection is empty or not.
If the Collection has items in it, the method will return false
like so:
1$collection = collect(['Ash', 'Allen']);2 3$collection->isEmpty(); // false
If the Collection is empty, the method will return true
like so:
1$collection = collect();2 3$collection->isEmpty(); // true
2. Using the isNotEmpty()
method
The Collection class also has an isNotEmpty
method that does the opposite of the isEmpty
method. It returns a boolean value indicating whether the Collection is empty or not.
If the Collection has items in it, the method will return true
like so:
1$collection = collect(['Ash', 'Allen']);2 3$collection->isNotEmpty(); // true
If the Collection is empty, the method will return false
like so:
1$collection = collect(['Ash', 'Allen']);2 3$collection->isNotEmpty(); // false
3. Using the count()
method
The count
method is a little different to the isEmpty
and isNotEmpty
methods. It returns the number of items in the Collection as an integer. But we can check against this integer to determine whether the Collection is empty or not.
If the Collection has items in it, the count
method will return an integer greater than 0, so we can check against this like so:
1$collection = collect(['Ash', 'Allen']);2 3$collection->count() === 0; // false
If the Laravel Collection is empty, the count
method will return 0, so we can check against this like so:
1$collection = collect();2 3$collection->count() === 0; // true
4. Using the count()
function
Similar to how we can use the count
method on the Collection class, we can also use the count
PHP function to check whether the Collection is empty or not. The function can be used to return the number of items in the Collection itself.
If the Collection has items in it, the count
function will return an integer greater than 0, so we can check against this like so:
1$collection = collect(['Ash', 'Allen']);2 3count($collection) === 0; // false
If the Laravel Collection is empty, the count
function will return 0, so we can check against this like so:
1$collection = collect();2 3count($collection) === 0; // true
5. Using the sizeof()
function
It's also possible to use the sizeof
PHP function to check whether the Collection is empty or not. However, this function is an alias of the count
, so it works in exactly the same way. Since it's an alias, your integrated development environment (IDE), such as PhpStorm, will likely suggest that you use the count
function instead. Some developers also prefer to avoid using sizeof
because they believe it gives the impression that it returns the size of the Collection in bytes.
However, if you do want to use the sizeof
function, you can still use it to check whether the Collection is empty or not.
If the Collection has items in it, the sizeof
function will return an integer greater than 0, so we can check against this like so:
1$collection = collect(['Ash', 'Allen']);2 3sizeof($collection) === 0; // false
If the Laravel Collection is empty, the sizeof
function will return 0, so we can check against this like so:
1$collection = collect();2 3sizeof($collection) === 0; // true
6. Using the blank()
function
A lesser-known method of checking whether a Collection is empty or not is to use Laravel's blank
function. This function isn't something I see used in a lot of projects, but can be quite handy in some situations.
The blank
function can be used to check whether many different types of values are empty or not. Let's take a quick look at how to use it and then we'll look at how it works under the hood.
If the Collection has items in it, the blank
function will return false
, so we can check against this like so:
1$collection = collect(['Ash', 'Allen']);2 3blank($collection); // false
If the Laravel Collection is empty, the blank
function will return true
, so we can check against this like so:
1$collection = collect();2 3blank($collection); // true
Now let's take a look at how the blank
function works under the hood. As of Laravel 10, the function looks like so:
1function blank($value) 2{ 3 if (is_null($value)) { 4 return true; 5 } 6 7 if (is_string($value)) { 8 return trim($value) === ''; 9 }10 11 if (is_numeric($value) || is_bool($value)) {12 return false;13 }14 15 if ($value instanceof Countable) {16 return count($value) === 0;17 }18 19 return empty($value);20}
As we can see, the function is relatively simple and will return true
if any of these conditions are true:
- The value is
null
- The value is a string and is empty
- The array implements the
Countable
interface and has a count of 0 - The value is empty
In the case of checking whether a Collection is empty, the function will use this part of the function to check whether the Collection is empty or not:
1function blank($value) 2{ 3 // ... 4 5 if ($value instanceof Countable) { 6 return count($value) === 0; 7 } 8 9 // ...10}
This is because the Laravel Collection class (\Illuminate\Support\Collection
) implements the \Illuminate\Support\Enumerable
interface which in turn extends the \Countable
interface. This means that the Collection class has a count
method that we can use to check whether the Collection is empty or not.
The Gotcha: Using the empty()
function and @empty
Blade directive
Now to the gotcha that has caught me out many times!
Sometimes when I want to check whether a PHP array is empty, I'll use the empty
function. This returns a boolean value indicating whether the array is empty or not. So I tend to try and use this function on Collections as well thinking it'll work in the same way. However, this is not the case.
If we try to use the empty
function on a Collection that contains some items, we'll get the expected result of false
:
1$collection = collect(['Ash', 'Allen']);2 3empty($collection); // false
If we try and use the empty
function on a Collection that doesn't contain any items, we'll get the unexpected result of false
:
1$collection = collect();2 3// โ This is not what we want!4empty($collection); // false
This is because the empty
function checks whether the value is empty or not. In the case of the Collection, the value is not empty because it's an object. This means that the empty
function will return false
even though the Collection is empty.
Similarly, if you're trying to use the @empty
Blade directive to check whether a Collection is empty or not, you'll also get the unexpected result of false
:
1@empty(collect())2 This will never be shown!3@else4 This will always be shown!5@endempty
For this reason, if you ever need to check whether a Collection is empty or not in a Blade template, you should use one of the other methods we've at in this article. For example, you could use the isEmpty
method like so:
1@if(collect()->isEmpty())2 This will be shown!3@else4 This will not be shown!5@endif
So this is definitely something to be aware of when you're checking if a Laravel Collection is empty.
Conclusion
Hopefully, this article has given you a quick insight into different ways that you can check whether a Laravel Collection is empty or not. It should have also highlighted the gotcha of using the empty
function on a Collection.
If you enjoyed reading this post, I'd love to hear about it. Likewise, if you have any feedback to improve the future ones, I'd also love to hear that too.
You might also be interested in checking out my 220+ page ebook "Battle Ready Laravel" which covers similar topics in more depth.
Or, you might want to check out my other 440+ ebook "Consuming APIs in Laravel" which teaches you how to use Laravel to consume APIs from other services.
If you're interested in getting updated each time I publish a new post, feel free to sign up for my newsletter below.
Keep on building awesome stuff! ๐