How To Check Array Is Empty Or Not In Laravel Blade?


In this tutorial, I will show you How to check array is empty or not in laravel. you will learn laravel check if array is empty. This article goes in detail on laravel not empty check. I would like to show you Laravel blade check if variable is empty.

So, let's follow this example to laravel check array empty in blade file with laravel 8 projects.

I simply read the documentation and I know the core PHP function so we can do it basically four-way to laravel check array not empty in laravel. so you can see below all examples one by one and you can use anyone that you want to use.

Laravel 8 - How To Check Array Is Empty Or Not In Laravel Blade?
Example 1: @forelse @empty

Controller Code:

public function index()
{
    $products = Product::get();
    return view('home',compact('products'));
}

Blade Code:

How To Check Array Is Empty Or Not In Laravel Blade? - phpcodingstuff.com
@forelse ($products as $product)

product

@empty

No product

@endforelse
Example 2: @empty

Controller Code:

public function index()
{
    $products = [];
    return view('home',compact('products'));
}

Blade Code:

How to check array is empty or not in laravel - phpcodingstuff.com
@empty($products)

product

@else

no product

@endempty
Example 3: @if Empty()

Controller Code:

public function index()
{
    $products = [];
    return view('home',compact('products'));
}

Blade Code:

Laravel blade check if variable is empty - phpcodingstuff.com
@if(empty($products))

product

@else

no product

@endif
Example 4: @if Count()

Controller Code:

public function index()
{
    $products = Product::get();;
    return view('home',compact('products'));
}

Blade Code:

check if array is empty in laravel blade - phpcodingstuff.com
@if($products->count() > 0)

product

@else

no product

@endif

I hope it can help you...

  1. In PHP, there are built-in methods for finding whether an array is empty or not. It is the developer’s choice for what function he/she will opt for checking the array. It is all about their requirements and preferences.

Leave a Reply

Your privacy will not be published. Required fields are marked *

We'll share your Website Only Trusted.!!

close