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

Home » Laravel » Laravel Check If Foreach is Empty Example

Laravel Check If Foreach is Empty Example

Laravel Check If Foreach is Empty Example

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

In this article, we will explain to you how to check If Foreach is Empty Example using laravel(Laravel Check If Foreach is Empty Example). so we will give you a simple example of laravel blade foreach empty example.

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

Read Also:
Laravel Check Array Empty in Blade

Example 1:@forelse @empty
In this example, we will use the @forelse and @empty to check the array in Laravel. we can easily check laravel array using @forelse and @empty. so you can see below the example code.
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 will use the @empty to check the array in Laravel. we can easily check laravel array using @empty. so you can see below the example code.
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>

Example 3: @if empty()
In this example, we will use the @if empty to check the array in Laravel. we can easily check laravel array using @if empty. so you can see below the example code.
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 will use the @if count to check the array in Laravel. we can easily check laravel array using @if count. so you can see below the example code.
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>

Please follow and like us:
error
fb-share-icon
Tweet
fb-share-icon

Related Posts:

  • Laravel Check Array Empty in Blade
  • Laravel 6 CRUD Operation With Ajax Example
  • Laravel 7 Multiple Authentication Example Tutorial
  • Laravel 5.8 crud tutorial example
  • Laravel 6 CRUD (Create Read Update Delete) Tutorial For…
  • Laravel 9 CRUD Operation Example Tutorial
Laravel Tags:check not empty in laravel blade, forelse empty laravel, laravel blade foreach empty, Laravel Blade Foreach If Empty or Not, laravel check if object is empty, laravel foreach empty

Post navigation

Previous Post: Laravel Check Array Empty in Blade
Next Post: How to enable and disable submit button using jQuery

Latest Posts

  • How to disable button in React js
  • JavaScript Interview Questions and Answers
  • JQuery Interview Questions and Answers
  • How to update php 7 to php 8 in xampp on windows
  • How to get the tag name in jQuery
  • how to get html tag in javascript
  • Disable Browser Back Button Using Javascript
  • How to remove file extension using htaccess
  • Matched leaf route at location does not have an element
  • Export ‘Switch’ (Imported As ‘Switch’) Was Not Found In ‘React-Router-Dom’ With Example

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 tricks 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 Socialite laravel social login nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor

Copyright © 2018 - 2023,

All Rights Reserved Powered by XpertPhp.com