Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Laravel Check Array Empty in Blade

Laravel Check Array Empty in Blade

Posted on August 13, 2022November 20, 2022 By XpertPhp

In this article, we will explain to you how to check array empty in blade using laravel(Laravel Check Array Empty in Blade). so we will give you simple example of Laravel Check Array Empty in Blade.

sometimes, we need to check if array is empty in blade using laravel. so you can easily check an array is empty or not in the blade using laravel.

If you want to check if the array is empty so the basic HTML markup will be displayed with no results inside or not empty then it will display the result. so you can see the below example.

Read Also:
Laravel Check If Foreach is Empty Example

Example 1:@forelse @empty
In this example, we use @forelse and @empty to check if the array is an empty array in laravel. We can easily check that the array is an empty array by using @forelse and @empty. so you can see the example code given in the following.
UserController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
 
class UserController extends Controller
{
 
    /**
     * Write Your Code..
     *
     * @return string
    */
    public function index()
    {
        $users = ['hitesh','pinak','ritesh'];
 
        return view('user.index',compact('users'));
    }
}
?>

resources/views/user/index.blade

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!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 - XpertPhp</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>

Example 2: @empty
In this example, we use @empty to check if the array is an empty array in laravel. We can easily check that the array is an empty array by using @empty. so you can see the example code given in the following.

UserController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
 
class UserController extends Controller
{
 
    /**
     * Write Your Code..
     *
     * @return string
    */
    public function index()
    {
        $users = ['hitesh','pinak','ritesh'];
 
        return view('user.index',compact('users'));
    }
}
?>

resources/views/user/index.blade

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!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 - XpertPhp</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>
See also  Laravel 7 Multiple Authentication Example Tutorial

Example 3: @if empty()
In this example, we use @if empty to check if the array is an empty array in laravel. We can easily check that the array is an empty array by using @if empty. so you can see the example code given in the following.
UserController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
 
class UserController extends Controller
{
 
    /**
     * Write Your Code..
     *
     * @return string
    */
    public function index()
    {
        $users = ['hitesh','pinak','ritesh'];
 
        return view('user.index',compact('users'));
    }
}
?>

resources/views/user/index.blade

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!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 - XpertPhp</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>

Example 4: @if count()
In this example, we use @if count to check if the array is an empty in laravel. We can easily check that the array is an empty array by using @if count. so you can see the example code given in the following.
UserController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
 
class UserController extends Controller
{
 
    /**
     * Write Your Code..
     *
     * @return string
    */
    public function index()
    {
        $users = ['hitesh','pinak','ritesh'];
 
        return view('user.index',compact('users'));
    }
}
?>

resources/views/user/index.blade

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!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 - XpertPhp</title>
</head>
<body>
    <div class="container">
        @if(count($users) > 0)
            <p class="bg-danger text-white p-1">users</p>
        @else
            <p class="bg-danger text-white p-1">no users</p>
        @endif
    </div>
</body>
</html>

Laravel Tags:Check If Array is Empty in Blade using Laravel, how to check array is empty or not in laravel blade, how to check array is empty or not in laravel controller, How to Check Array is Empty or Not in Laravel?, laravel check if array is empty Example

Post navigation

Previous Post: PHP Datatables CRUD Example
Next Post: Laravel Check If Foreach is Empty Example

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com