How to Check If Collection is Empty in Laravel?
Hi Developer,
In this quick guide, we will teach you how to check if collection is empty laravel. It's a simple example of laravel collection check if empty. we will help you to give an example of check if empty collection laravel. if you want to see an example of check if collection is empty laravel blade then you are in the right place.
You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions.
There are several ways to check laravel collection is empty or not. I will give you the following list of examples that will check if the collection is empty in laravel.
Without further ago, please check the below examples code:
Example 1: Using isEmpty()
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$posts = Post::get();
if (!$posts->isEmpty()) {
dd("posts eloquent collection is not empty.");
}else{
dd("posts eloquent collection is empty.");
}
}
}
Example 2: Using count()
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$posts = Post::get();
if ($posts->count()) {
dd("posts eloquent collection is not empty.");
}else{
dd("posts eloquent collection is empty.");
}
}
}
Example 3: Using first()
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$posts = Post::get();
if ($posts->first()) {
dd("posts eloquent collection is not empty.");
}else{
dd("posts eloquent collection is empty.");
}
}
}
Example 4: Using isNotEmpty()
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$posts = Post::get();
if ($posts->isNotEmpty()) {
dd("posts eloquent collection is not empty.");
}else{
dd("posts eloquent collection is empty.");
}
}
}
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Check Database Connection in Laravel?
- How to Use Google Translator in Laravel?
- How to Convert Collection to JSON in Laravel?
- How to Install Laravel in Ubuntu Server?
- How to Get Specific Attributes from Laravel Collection?
- Laravel Collection keyBy() Method Example
- Laravel Collection Has Method Example
- Laravel Collection Flip Method Example
- Laravel Collection Duplicates Method Example
- Laravel Collection first() and firstWhere() Methods Example
- Laravel Collection Push() and Put() Example
- Laravel Collection Merge | How to Merge Two Eloquent Collection?