Don’t do this mistake in the Laravel collection
Avoid using the empty function in the Laravel collection
The Laravel collection is a wrapper of a PHP array. In Laravel world, it was a new data type created based on the array. The Illuminate\Support\Collection class provides the Laravel collection. Also, the Laravel Eloquent methods will return the collection instance.
The results of Eloquent queries are always returned as
Collectioninstances.
Compare to the array, the collection methods make your life easier. Click here to check the available collection methods on the Laravel document.
Thecollect helper is used to creating the collection based on the given array.
$collection = collect([1, 2, 3]);
dd($collection);If will get the below result when you print the collection.

Before checking the empty function issue, I am going to clarify some common questions in the Laravel collection.
Can we use foreach iterators?
Yes, we can use foreach instead of each method for the collection.
$collection = collect([1, 2, 3]);
foreach ($collection as $data) {
echo $data;
}
$collection->each(function ($item) {
echo $item;
});How to convert the PHP collection to an array?
The toArray() function converts the collection into a plain PHP array.
$collection = collect([1, 2, 3]);
$array_colection = $collection->toArray();
dd($array_colection);
We know the array method foreach is working fine with the collection. But all the array methods we can't use with the collection.
Don’t use the empty
The most common mistake is checking collection is empty by using the empty method. Below the example, created an empty collection and added an if statement to check the collection is empty.
$collection = collect([]);
if(empty($collection)) {
dd('collection is empty');
}Also, don’t use the empty for Eloquent result
$users = User::where('active', 1)->get();
if(empty($users)) {
dd('collection is empty');
}The collection is empty but it does not pass the conditional statement. Because the collection is not empty, because it has the empty items array. Let’s print the empty collection.
$collection = collect([]);
dd($collection);
So use the isEmpty() or isNotEmpty() collection methods to check the collections.
$collection = collect([]);
if($collection->isEmpty()) {
dd('collection is empty');
}
if($collection->isNotEmpty()) {
dd('collection is empty');
}Can we use count and isset?
I faced an issue with an empty function. So I decided to test with count and isset functions.
$collection = collect(['name' => 'Balaji']);
if(isset($collection['name'])) {
dd($collection);
}The isset() function is working as expected.
$collection = collect([1, 2, 3]);
if(count($collection) > 2) {
dd(count($collection));
}Yes, the array count method also worked fine. But better to use the collection count() to get the count.
$collection = collect([1, 2, 3]);
if($collection->count() > 2) {
dd($collection->count());
}Conclusion
We know the collection is a wrapper of the array. Also, the collection has a lot of functions and it makes it easy to search, filter, loop, find, etc.,
So my suggestion is don’t try the empty, count, and isset with collections. Use the corresponding collection functions like $collection->isEmpty() $collection->count()
How are collection functions are working?
Do you think how is collection function $collection->isEmpty() $collection->count() is working? The answer is collection also uses array functions, but they use array functions for collection items.
This below collection function is declared on Illuminate\Support\Collection
/**
* Determine if the collection is empty or not.
*
* @return bool
*/
public function isEmpty()
{
return empty($this->items);
}
/**
* Count the number of items in the collection.
*
* @return int
*/
public function count(): int
{
return count($this->items);
}Refer to the file: https://github.com/laravel/framework/blob/master/src/Illuminate/Collections/Collection.php
Thank you for reading.
Stay tuned for more!
Follow me at balajidharma.medium.com.






