Laravel Check Array Empty in Blade Examples Tutorial

Reading Time: 6 minutes
885 Views

Inside this article we will see the concept i.e Laravel Check Array Empty in Blade Examples Tutorial. Article contains the classified information about Laravel – How to Check If Array is Empty in Blade?

If you are looking for a solution i.e Check If Array is Empty in Blade using Laravel then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Article is not for laravel specific version, you can use this concept with any laravel version.

Read More: Laravel Blade Check if Array Key Exists Example Tutorial

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Example #1: Using @forelse and @empty

Here, we will use @forelse and @empty to check if the array is an empty array in laravel. This code snippet is super easy to understand and also to add a check that the array is an empty array or not.

Read More: Laravel Blade Check If Variable Exists or Not Example

Let’s consider a controller i.e UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{

    /**
     * Write Your Code..
     *
     * @return string
    */
    public function index()
    {
        $users = ['sanjay','siddharth','mrituanjay'];

        return view('user.index',compact('users'));
    }
}

Next,

To create a view file: Go to /resources/views folder. Create a folder called user. Inside that folder create a file with name index.blade.php

Code of blade view file:

<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Laravel Check Array Empty in Blade Example Tutorial</title>
</head>
<body>
    <div class="container">
        @forelse ($users as $user)
            <p class="bg-danger text-white p-1">user</p>
        @empty
            <p class="bg-danger text-white p-1">No user</p>
        @endforelse
    </div>
</body>
</html>
  

This is how you can use laravel directives @forelse and @empty to check If Array is Empty in Blade using Laravel.

Example #2: Using @empty

Here, we will use @empty to check if the array is an empty array in laravel.

Read More: Laravel 9 How To Update Multiple Records Example Tutorial

This code snippet is super easy to understand and also to add a check that the array is an empty array or not.

Let’s consider a controller i.e UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{

    /**
     * Write Your Code..
     *
     * @return string
    */
    public function index()
    {
        $users = ['sanjay','siddharth','mrituanjay'];

        return view('user.index',compact('users'));
    }
}

Next,

To create a view file: Go to /resources/views folder. Create a folder called user. Inside that folder create a file with name index.blade.php

Code of blade view file:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Laravel Check Array Empty in Blade Example Tutorial</title>
</head>
<body>
    <div class="container">
        @empty($users)
            <p class="bg-danger text-white p-1">no user</p>
        @else
            <p class="bg-danger text-white p-1">user</p>
        @endempty
    </div>
</body>
</html>
  

This is how you can use laravel directive @empty to check If Array is Empty in Blade using Laravel.

Example #3: Using @if empty()

Here, we will use @if empty() to check if the array is an empty array in laravel.

Read More: Laravel 9 Language Translation Methods Example Tutorial

This code snippet is super easy to understand and also to add a check that the array is an empty array or not.

Let’s consider a controller i.e UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{

    /**
     * Write Your Code..
     *
     * @return string
    */
    public function index()
    {
        $users = ['sanjay','siddharth','mrituanjay'];

        return view('user.index',compact('users'));
    }
}

Next,

To create a view file: Go to /resources/views folder. Create a folder called user. Inside that folder create a file with name index.blade.php

Code of blade view file:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Laravel Check Array Empty in Blade Example Tutorial</title>
</head>
<body>
    <div class="container">
        @if(empty($users))
            <p class="bg-danger text-white p-1">no user</p>
        @else
            <p class="bg-danger text-white p-1">user</p>
        @endif
    </div>
</body>
</html>
  

This is how you can use laravel directive @if empty() to check If Array is Empty in Blade using Laravel.

We hope this article helped you to learn Laravel Check Array Empty in Blade Examples Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more