Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check if a Laravel collection is empty?
Before checking if a Laravel collection is empty, let's understand what collections are in Laravel. Collection in Laravel is an API wrapper that helps you deal with different operations on arrays. It uses the class Illuminate\Support\Collection to handle arrays in Laravel.
To create a collection from an array, use the collect() helper method that returns a collection instance. You can then chain methods like converting to lowercase and sorting on the collection.
Installation: Make sure you have Laravel installed and create a controller using:
php artisan make:controller UserController
Using the isEmpty() Method
The isEmpty() method returns true when the collection is empty and false if it's not ?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
class UserController extends Controller {
public function index() {
$mynames = collect([]);
if ($mynames->isEmpty()) {
return "Collection is Empty";
} else {
return "Collection is Not Empty";
}
}
}
Collection is Empty
Using the isNotEmpty() Method
The isNotEmpty() method returns true if the collection is not empty and false if empty ?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
class UserController extends Controller {
public function index() {
$mynames = collect([]);
if (!$mynames->isNotEmpty()) {
return "Collection is Empty";
} else {
return "Collection is Not Empty";
}
}
}
Collection is Empty
Using the count() Method
The count() method returns the number of items inside the collection ?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
class UserController extends Controller {
public function index() {
$mynames = collect([]);
if ($mynames->count() === 0) {
return "Collection is Empty";
} else {
return "Collection is Not Empty";
}
}
}
Collection is Empty
Using the first() Method
The first() method returns the first element from the collection. For empty collections, it returns null ?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
class UserController extends Controller {
public function index() {
$mynames = collect([]);
if (!$mynames->first()) {
return "Collection is Empty";
} else {
return "Collection is Not Empty";
}
}
}
Collection is Empty
Comparison
| Method | Returns | Best Use Case |
|---|---|---|
isEmpty() |
Boolean | Most readable and direct |
isNotEmpty() |
Boolean (opposite) | When checking for non-empty |
count() |
Integer | When you need the exact count |
first() |
Mixed/null | Less recommended for empty checks |
Conclusion
The isEmpty() method is the most direct and readable way to check if a Laravel collection is empty. Use count() when you need the exact number of items, and isNotEmpty() for checking non-empty conditions.
