Php

Laravel Check Collection Method | isEmpty() and isNotEmpty()

This laravel quick tutorial helps to understand Laravel collection is empty or not. The laravel collection is used to store data with arrays of data. The collections are immutable, which Means every Collection method returns an entirely new Collection instance.

Immutable collections can’t be changed at all. They have their own elements. The laravel collection will have two methods to check collection is empty or not.

You can also check other recommended tutorials of Lumen/Laravel,

You can use these methods with any version laravel 8 and laravel 9 applications. I will give you simple examples of isEmpty() and isNotEmpty() collection in laravel.

Laravel Collection isEmpty() Example

Let’s take a simple collection example to check is empty or not using isEmpty() method.

Related Post

Syntax:

$collecton->isEmpty();

Laravel isEmpty() Example

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

Output:

true

Laravel Collection isNotEmpty() Example

Let’s check laravel collection is not empty using isNotEmpty() method.

Syntax:

$collecton->isNotEmpty();

Laravel isNotEmpty Example

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

Output:

false

Recent Posts

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

2 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

2 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

3 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

3 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

3 months ago

Common Practices for Laravel Blade Template

in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More

4 months ago

Categories