Introducing

Laravel how to check if collection is empty


If you need to check if a collection is empty in Laravel, there are a few different ways you can do this.

One way is to use the count() method:

$collection = collect([1, 2, 3]);

if ($collection->count()) {
// The collection is not empty...
}

Another way is to use the isEmpty() method:

$collection = collect([]);

if ($collection->isEmpty()) {
// The collection is empty...
}

You can also use the empty() function:

$collection = collect([]);

if (empty($collection)) {
// The collection is empty...
}

Finally, you can use the count() method on the collection itself:

$collection = collect([]);

if ($collection->count()) {
// The collection is not empty...
}

So there you have it - four different ways to check if a collection is empty in Laravel.

Categories:
Tags:
Laravel
collection