How to check a collection isempty in laravel?

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

How to check a collection isempty in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by shyann , 6 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".