ItSolutionStuff.com

Laravel Check If Foreach is Empty Example

By Hardik Savani • April 16, 2024
Laravel

Hi Artisan,

Are you looking for example of laravel blade foreach if emptlaravel blade foreach if empty. if you have question about laravel blade foreach if not empty then i will give simple example with solution.

Here you will learn laravel blade foreach empty. you can see laravel foreach if empty. follow bellow step for how to check if foreach is empty in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 app.

i simply read documentation and i know core php function so we can do it basically four way to laravel check array empty in blade. so you can see bellow all example one by one and you can use anyone that you want to use.

Example 1: @forelse @empty

Controller Code:

public function index()

{

$products = Product::get();

return view('home',compact('products'));

}

Blade Code:

<div class="card-header">

<h5>Laravel Check Array Empty in Blade - itsolutionstuff.com</h5>

</div>

<div class="card-body">

@forelse ($products as $product)

<p class="bg-danger text-white p-1">product</p>

@empty

<p class="bg-danger text-white p-1">No product</p>

@endforelse

</div>

Example 2: @empty

Controller Code:

public function index()

{

$products = [];

return view('home',compact('products'));

}

Blade Code:

<div class="card-header">

<h5>Laravel Check Array Empty in Blade - itsolutionstuff.com</h5>

</div>

<div class="card-body">

@empty($products)

<p class="bg-danger text-white p-1">product</p>

@else

<p class="bg-danger text-white p-1">no product</p>

@endempty

</div>

Example 3: @if empty()

Controller Code:

public function index()

{

$products = [];

return view('home',compact('products'));

}

Blade Code:

<div class="card-header">

<h5>Laravel Check Array Empty in Blade - itsolutionstuff.com</h5>

</div>

<div class="card-body">

@if(empty($products))

<p class="bg-danger text-white p-1">product</p>

@else

<p class="bg-danger text-white p-1">no product</p>

@endif

</div>

Example 4: @if count()

Controller Code:

public function index()

{

$products = Product::get();;

return view('home',compact('products'));

}

Blade Code:

<div class="card-header">

<h5>Laravel Check Array Empty in Blade - itsolutionstuff.com</h5>

</div>

<div class="card-body">

@if($products->count() > 0)

<p class="bg-danger text-white p-1">product</p>

@else

<p class="bg-danger text-white p-1">no product</p>

@endif

</div>

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Write PHP Code in Laravel Blade?

Read Now →

Laravel - How to Check If Array is Empty in Blade?

Read Now →

Laravel Bail Rule | Stop Validation On First Failure

Read Now →

Laravel Chartjs Chart Example Tutorial

Read Now →

How to Get Base URL in Laravel?

Read Now →

Laravel Clear Cache from Route, View, Config Example

Read Now →

Laravel Redirect Back to Previous Page After Login Example

Read Now →