Laravel Check Array Empty in Blade

10-Apr-2023

.

Admin

Laravel Check Array Empty in Blade

Hi Guys

This tutorial will give you example of laravel check if array is empty in blade. Here you will learn laravel blade check array empty. This article will give you simple example of check empty array in laravel blade. if you want to see example of laravel if array empty then you are a right place.

I will show check the if array is empty in blade. we will explain diff function of check array is empty in blade.An empty array can sometimes cause software crash or unexpected outputs. To avoid this, it is better to check whether an array is empty or not beforehand. There are various methods and functions available in laravel to check whether the defined or given array is an empty or not in blade.

Here the example of check if array is empty in laravel.

Example 1


In this example I will show using @forelse loop in laravel.

Controller file Code

public function index()

{

$products = Product::get();

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

}

blade file code

<div class="container">

<div class="col-md-6 offset-md-3">

<div class="card mt-5">

<div class="card-header">

<h5>Laravel Check Array Empty in Blade-nicesnippets.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>

</div>

</div>

</div>

Example 2

In this example I will show using @empty method in laravel.

Controller file Code

public function index()

{

$products = [];

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

}

blade file code

<div class="container">

<div class="col-md-6 offset-md-3">

<div class="card mt-5">

<div class="card-header">

<h5>Laravel Check Array Empty in Blade-nicesnippets.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>

</div>

</div>

</div>

Example 3

In this example I will show using empty function in laravel.

Controller file Code

public function index()

{

$products = [];

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

}

blade file code

<div class="container">

<div class="col-md-6 offset-md-3">

<div class="card mt-5">

<div class="card-header">

<h5>Laravel Check Array Empty in Blade-nicesnippets.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>

</div>

</div>

</div>

Example 4

In this example I will show using count function in laravel.

Controller file Code

public function index()

{

$products = Product::get();

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

}

blade file code

<div class="container">

<div class="col-md-6 offset-md-3">

<div class="card mt-5">

<div class="card-header">

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

</div>

<div class="card-body">

@if(count($products) > 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>

</div>

</div>

</div>

It will help you...

#Laravel 7

#Laravel

#Laravel 6