- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to 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
- Related Articles
- How to check if a cookie is set in Laravel?
- How to check empty field in a MongoDB collection?
- How to check if a C# list is empty?
- C# Program to Check a HashTable collection is empty or not
- Golang program to check a hash collection is empty or not
- How to check if android editText is empty?
- How to check if a list is empty in Python?
- How to check if a string is empty in Kotlin?
- Golang to check if a slice is empty
- How to check if a user email already exists in Laravel?
- How to check if String is empty in Java?
- How to check if PSCustomObject is empty in PowerShell?
- How to add a new value to a collection in Laravel?
- Golang program to check if a stack is empty
- Swift Program to Check if a Set is empty
