How to Check Array is Empty or Not in Laravel?

Aug 10, 2022

.

Admin

How to Check Array is Empty or Not in Laravel?

Hello Friends,

In this article we will cover on how to check array is empty or not in laravel. if you have question about laravel check array empty in blade then I will give simple example with solution. This post will give you simple example of laravel check if array is empty in controller. I’m going to show you about how to check array is empty or not in laravel controller.

i simply read documentation and i know core php function so we can do it basically four way to laravel check array empty in blade and controller file. so you can see bellow all example one by one and you can use anyone that you want to use.

You can use this example with laravel 6, laravel 7, laravel 8 and laravel 9 version.

so let's start with following examples

Download Laravel


Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app

Check Array is Empty or Not in Controller File:

Example 1 :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller

{

/**

* Write Your Code..

*

* @return string

*/

public function index()

{

$arry = ['nikhil','vishal'];

if(count($arry) > 0){

dd('EMPTY');

}

else{

dd('Not EMPTY');

}

}

}

Output:

Not EMPTY

Example 2 :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller

{

/**

* Write Your Code..

*

* @return string

*/

public function index()

{

$arry = ['nikhil','vishal'];

if(isset($arry) && count($arry)>0){

dd('arry is not empty');

}else

{

dd('arry is empty');

}

}

}

Output:

Not EMPTY

Example 3 :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller

{

/**

* Write Your Code..

*

* @return string

*/

public function index()

{

$arry = ['nikhil','vishal'];

if(empty($arry)){

dd('EMPTY');

}else{

dd('NOT EMPTY');

}

}

}

Output:

Not EMPTY

Example 4 :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller

{

/**

* Write Your Code..

*

* @return string

*/

public function index()

{

$arry = ['nikhil','vishal'];

if(sizeof($arry) == 0){

dd('EMPTY');

}else{

dd('NOT EMPTY');

}

}

}

Output:

Not EMPTY

Check Array is Empty or Not in Blade File:

Example 1 :

Controller Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller

{

/**

* Write Your Code..

*

* @return string

*/

public function index()

{

$users = ['nikhil','vishal'];

return view('post.index',compact('users'));

}

}

Blade Code:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>How to Check Array is Empty or Not in Laravel? - Nicesnippets.com</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>

Output:

user

Example 2 :

Controller Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller

{

/**

* Write Your Code..

*

* @return string

*/

public function index()

{

$users = ['nikhil','vishal'];

return view('post.index',compact('users'));

}

}

Blade Code:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>How to Check Array is Empty or Not in Laravel? - Nicesnippets.com</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>

Output:

user

Example 3 :

Controller Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller

{

/**

* Write Your Code..

*

* @return string

*/

public function index()

{

$users = ['nikhil','vishal'];

return view('post.index',compact('users'));

}

}

Blade Code:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>How to Check Array is Empty or Not in Laravel? - Nicesnippets.com</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>

Output:

user

Example 4 :

Controller Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller

{

/**

* Write Your Code..

*

* @return string

*/

public function index()

{

$users = ['nikhil','vishal'];

return view('post.index',compact('users'));

}

}

Blade Code:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>How to Check Array is Empty or Not in Laravel? - Nicesnippets.com</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>

Output:

users

now it works...

I hope it can help you...

#Laravel