isEmpty() vs isNotEmpty() Laravel Collection Method Example

Sovary September 17, 2022 467
1 minute read

Today we will give example quick and short of Laravel collection to check if empty data as well as is not empty. I will share with you to check if collection is empty or not empty in Laravel. The examples below will give you understand more clearly. Let check below Laravel example of collection to check empty collrection or not.

There are some methods exist to check the collection. We will use Laravel checkcollection is not empty. So this article will help you to understand how to use functions to check laravel collection empty. Please check below example laravel collection isNotEmpty and isempty(). You can easily use it with your Laravel 5, Laravel 6, Laravel 7, Laravel 8 and Laravel 9 application as well.

Laravel Collection isEmpty()

Syntax:

$collecton->isEmpty();

Example:

public function index()
{
    $collection = collect([]); 
    $rs = $collection->isEmpty();
    dd($rs);
}

Output:

true

Laravel Collection isNotEmpty()

Syntax:

$collecton->isNotEmpty();

 Example:

public function index()
{
    $collection = collect([]);
    $rs= $collection->isNotEmpty();
    dd($rs);
}

Output: 

false

 

You might also like...

 

Laravel  PHP  Laravel 9  Laravel Collections 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him