
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a Laravel Collection is Empty
Before we answer the above question, let us first understand what collections are in Laravel. Collection in Laravel is an API wrapper that helps you deal with different operations to be performed on arrays. It makes use of the class Illuminate\Support\Collection to deal with arrays in Laravel.
To create a collection from a given array you need to make use of the collect() helper method that returns a collection instance. Later you can use a chain of methods like converting to lowercase and sorting on the collection instance.
Example
The example in this section demonstrates how to create a collection from a given array. Before proceeding with it make sure you have created a controller of your own if not, create one using the artisan make command as ?
php artisan make:controller UserController
This will create a controller in the path "Http/Controllers". Copy the following code and paste it in the created controller.
UserController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Collection; class UserController extends Controller { public function index() { $mynames = collect(['Andria', 'Josh', 'James', 'Miya', 'Henry']); print_r($mynames); } }
Web.php
We need to create/register a route to call the function index() in the "resources/routes/web.php" file as ?
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\UserController; Route::get('/', function () { return view('welcome'); }); Route::get('getData', [UserController::class,'index' ] );
Once you create these two files run the server as ?
php artisan serve
Output
If you test the above program by sending request to the URL http://127.0.0.1:8000/getData it will generate the following output
Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => Andria [1] => Josh [2] => James [3] => Miya [4] => Henry ) [escapeWhenCastingToString:protected] => )
There are various methods available to see if the collection is empty. They are ?
Using the isEmpty() method
The isEmpty() method returns true/false. It's true when the collection is empty and false if it's not. Following is the syntax of this method ?
isEmpty()
Example
Following example verifies whether a given collection is empty using the isEmpty() method ?
<?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"; } } }
Replace the contents of the UserController.php with the above code clear the Route cache as ?
php artisan route:clear
Output
If you test the above program by sending request to the URL http://127.0.0.1:8000/getData it will generate the following output -
Collection is Empty
Using the isNotEmpty() method
The isNotEmpty() method returns true if the collection tested is not empty and false if empty. Following is the syntax of this method -
isNotEmpty()
Example
Following example verifies whether a given collection is empty using the isNotEmpty() method ?
<?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"; } } }
Output
Collection is Empty
Using the count() method
The count() method returns the number of items inside the collection. Following id the syntax of it ?
count()
Example
Following example verifies whether a given collection is empty using the count() method ?
<?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"; } } }
Output
Collection is Empty
Using the first() Method
The first() method returns the first element from the collection.
first()
Example
You can make use of first() to know if the collection is empty or not as shown in the following example ?
<?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"; } } }
Output
Collection is Empty
Using the count() method
The count() method returns the number of items in the given collection. Following is the syntax of this method
count($collection)
Example
You can use this method to check if the collection is empty as shown below ?
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Collection; class UserController extends Controller { public function index() { $mynames = collect([]); if (count($mynames) === 0) { return "Collection is Empty"; } else { return "Collection is Not Empty"; } } }
Output
Collection is Empty