Laravel - How to Check If Array is Empty in Blade?
In this tutorial, i will show you laravel check array empty in blade. you will learn laravel check if array is empty. This article goes in detailed on laravel not empty check. i would like to show you check empty array in laravel blade.
So, let's follow this example to check if array is empty in blade file with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 project.
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 8/7 Paginate with Collection or Array
- Merge Multiple Collection Paginate in Laravel
- Laravel Blade Check if View Exists or Not Example
- How to Get User Agent Value in Laravel?
- Laravel Calculate Distance Between Two Latitude and Longitude Example
- Laravel Order By with Column Value Example
- How to Count Files in a Directory using Laravel?
- Mysql procedure with pagination in laravel?