How to Check if a Variable is Empty in Laravel Blade?

Nov 29, 2022 . Admin

Hello Friends,

Here, I will show you how to check if a variable is empty in laravel blade. if you have a question about laravel check in blade file if variable is empty or not example then I will give a simple example with a solution. I want to share with you laravel check in blade file if variable is empty. I explained simply about the how to check in blade file if variable is empty in laravel. Let's get started with the laravel check in blade file if variable is empty with an example.

You can use this example with the versions of laravel 6, laravel 7, laravel 8, and laravel 9.

You have just to follow the below step and you will get the layout as below:

Step 1: Install Laravel

This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app
Step 2: Create Format Route web.php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\EmptyVariableController;

| Web Routes
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!

Route::get('/index',[EmptyVariableController::class, 'index']);
Step 3: create EmptyVariableController

we will create EmptyVariableController. so you can see the below code with output:

php artisan make:controller EmptyVariableController

namespace App\Http\Controllers;
use Illuminate\Http\Request;

class EmptyVariableController extends Controller
     * The attributes that are mass assignable.
     * @var array
    public function index()
        $data = null;

        return view('index', compact('data'));
Step 4: Create Blade File index.blade.php resources/views/index.blade.php
<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>How to Check if a Variable is Empty in Laravel Blade:</title>
        {{ dd('It\'s an empty variable') }}  

        {{ dd('It\'s not empty variable') }}

Step 5: Start Development Server

Start the development server. Use the PHP artisan serve command and start your server:

php artisan serve

Now you are ready to run our example so run the below command to quick run.

"It's an empty variable" // resources/views/index.blade.php

I hope it can help you...