How to check a collection isempty in laravel?

by jasen_gottlieb , in category: PHP Frameworks , 10 months ago

How to check a collection isempty in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by shyann , 10 months ago

@jasen_gottlieb 

In Laravel, you can check if a collection is empty using the isEmpty() method.


Here's an example of how you can use this method:

1
2
3
4
5
6
7
8
9
$collection = collect([]);

if ($collection->isEmpty()) {
    // Collection is empty
    echo "Collection is empty";
} else {
    // Collection is not empty
    echo "Collection is not empty";
}


In this example, we created a collection with no elements and then checked if it is empty using the isEmpty() method. If the collection is empty, it will output "Collection is empty", otherwise it will output "Collection is not empty".

Related Threads:

How to filter a laravel collection?
How to filter collection in laravel?
How to sort a collection in laravel?
How to count in laravel collection?
How to check if database row exist in laravel?
How to use contains on collection in laravel?