How to Check If Collection is Empty in Laravel? (2022)

Hello dev, Today we are going to learn How to Check If Collection is Empty in Laravel? This tutorial will cover on how you can check if the collection is empty or not in laravel application.

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 and laravel 9 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:

Also Read : How to Get Database Name in Laravel 2022?

Examples on How to Check If Collection is Empty in Laravel?

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.");
        }
    }
}

Also Read : How to Check Database Connection in Laravel 2022?

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.");
        }
    }
}

Also Read : How to Get All Files in a Directory in Laravel (2022)

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.");
        }
    }
}

Question also asked

  • How do I check if a collection is empty in laravel?
  • How do I know if a collection is empty?
  • How do you know if eloquent results are empty?
  • Is null in laravel eloquent?

Conclusion

Today, We had learn How to Check If Collection is Empty in Laravel?. Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.

Also Read : How to Get All Routes in Laravel 9 ? [ New / Free Tutorials ]

2 Comments

Leave a Reply