Laravel check if array is empty in blade?

In this tutorial explained of laravel check if array is empty in blade. You will learn laravel check if array is empty. I would like to show you check empty array in laravel blade.

In this post we’re going to take look at some of the most common method to check if collection is empty.

1. Laravel check if array is empty in blade using isEmpty() method:

You can use this method directly laravel collection class. It does exactly what it says on the tin and returns a boolean value indicating wheather the collection is empty or not. You can consider following example.

If the collection has items in it, below example will return false like so:

$collection = collect(['test', 'grade']);

$collection->isEmpty(); // false

If the collection has items in it, below example will return true like so:

$collection = collect();

$collection->isEmpty(); // true
2. Laravel check if array is empty in blade using @forelse @empty
Controller code:
public function index()
{
    $items = Items::get();
    return view('test', compact('items'));
}
Blade code:
<div class="card">
    <div class="card-header">
        <h5>Laravel Check if array is empty in blade - webstuffsolution.com</h5>
    </div>
    <div class="card-body">
        @forelse($items as $item)
        <p class="bg-success text-white p-1">item</p>
        @empty
        <p class="bg-danger text-white p-1">No item</p>
        @endforelse
    </div>
</div>
3. Laravel check if array is empty in blade using @empty
Controller code:
public function index()
{
    $items = [];
    return view('home', compact('items'));
}
Blade code:
<div class="card">
    <div class="card-header">
        <h5>Laravel Check if array is empty in blade - webstuffsolution.com</h5>
    </div>
    <div class="card-body">
        @empty($items)
        <p class="bg-success text-white p-1">item</p>
        @else
        <p class="bg-danger text-white p-1">No item</p>
        @endempty
    </div>
</div>
4. Laravel check if array is empty in blade using @if empty()
Controller code:
public function index()
{
    $items = [];
    return view('home', compact('items'));
}
Blade code:
<div class="card">
    <div class="card-header">
        <h5>Laravel Check if array is empty in blade - webstuffsolution.com</h5>
    </div>
    <div class="card-body">
        @if(!empty($items))
        <p class="bg-success text-white p-1">item</p>
        @else
        <p class="bg-danger text-white p-1">No item</p>
        @endempty
    </div>
</div>
5. Laravel check if array is empty in blade using @if count()
Controller code:
public function index()
{
    $items = Item::get();
    return view('item', compact('items'));
}
Blade code:
<div class="card">
    <div class="card-header">
        <h5>Laravel Check if array is empty in blade - webstuffsolution.com</h5>
    </div>
    <div class="card-body">
        @if($items->count() > 0)
        <p class="bg-success text-white p-1">item</p>
        @else
        <p class="bg-danger text-white p-1">No item</p>
        @endif
    </div>
</div>

I hope this tutorial help you.

Leave a Reply

Your email address will not be published. Required fields are marked *