Laravel Blade Check If Variable Exists or Not Example

Share this Article
Reading Time: 6 minutes
396 Views

Inside this article we will see the concept i.e Laravel Blade Check If Variable Exists or Not Example Tutorial. Article contains the classified information about How to check if variable is defined in laravel.

If you are looking for a solution i.e Check if variable exists in laravel blade then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Article is not for laravel specific version, you can use this concept with any laravel version.

There are several ways to check if the variable exists or not in laravel blade file. We will see some examples to check if a variable exists in laravel blade file or not. We will use @isset, @if and @empty directive.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

$ composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Example #1: Using @isset Directive

To check if variable is defined in laravel blade or not, we will use @isset() directive.

Let’s consider a controller i.e UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{

    /**
     * Write Your Code..
     *
     * @return string
    */
    public function index()
    {
        $user = "Sanjay Kumar";

        return view('user.index',compact('user'));
    }
}

Next,

To create a view file: Go to /resources/views folder. Create a folder called user. Inside that folder create a file with name index.blade.php

Code of blade view file:

<!DOCTYPE html>
<html>
<head>
  <title>Laravel Blade Check If Variable Exists or Not Example</title>
</head>
<body>
    
@isset($user)
    {{ $user }}
@endisset
    
</body>
</html>
  

Here, the above code checks $user variable exists or not in blade view file. If it is defined then it will print the value else no output you will get.

Example #2: Using @if Directive

To check if variable is defined in laravel blade or not, we will use @if() directive.

Again, let’s consider a controller i.e UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{

    /**
     * Write Your Code..
     *
     * @return string
    */
    public function index()
    {
        $user = "Sanjay Kumar";

        return view('user.index',compact('user'));
    }
}

Next,

To create a view file: Go to /resources/views folder. Create a folder called user. Inside that folder create a file with name index.blade.php

Code of blade view file:

<!DOCTYPE html>
<html>
<head>
  <title>Laravel Blade Check If Variable Exists or Not Example</title>
</head>
<body>
    
@if(isset($user))
    {{ $user }}
@endif
    
</body>
</html>
  

Here, the above code checks $user variable exists or not in blade view file. If it is defined then it will print the value else no output you will get.

Example #3: Using @empty Directive

To check if variable is defined in laravel blade or not, we will use @empty() directive.

Again, let’s consider a controller i.e UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{

    /**
     * Write Your Code..
     *
     * @return string
    */
    public function index()
    {
        $users = ['sanjay','siddharth','mrituanjay'];

        return view('user.index',compact('users'));
    }
}

Next,

To create a view file: Go to /resources/views folder. Create a folder called user. Inside that folder create a file with name index.blade.php

Code of blade view file:

<!DOCTYPE html>
<html>
<head>
  <title>Laravel Blade Check If Variable Exists or Not Example</title>
</head>
<body>
    
@empty($users)
    {{ $users }}
@endempty
    
</body>
</html>
  

Here, the above code checks $users variable exists or not in blade view file. If it is defined then it will print the value else no output you will get.

We hope this article helped you to learn Laravel Blade Check If Variable Exists or Not Example Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

PHP Web Development Courses on Offer in just $4
PHP Web Development Courses on Offer in just $4