Laravel Check If Foreach is Empty Example
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.
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> |