The common data storage method in Laravel is collection. The Collection class allows you to chain its methods to perform fluent mapping and reducing of the underlying array. It provides most methods like array methods.
There are many methods to check whether the collection is empty or not with examples. Let's know more about them one by one with examples.
Using isEmpty() Method
The isEmpty() method checks whether specifically, the collection is empty or not and returns boolean as output.
Using count() Method
The count() method returns data count in a collection so we can perform the condition that if count is greater than 0 then the collection object is not empty. Let's take an example of the count method to check collection is empty or not:
Using first() Method
The first method returns the first data from the collection so here we apply the logic that if the collection has one or the first data then it's not empty otherwise it's empty.
Using isNotEmpty() Method
The isNotEmpty() method is a reverse method of isEmpty(). As the name suggests the isNotEmpty() method checks collection has data. so we can apply further logic to it.
Conclusion
In this article, we have taken a few examples to check Laravel collection is empty or not. Here, we have seen 4 methods to check collection but the idle method for this concept is the isEmpty() method.