Laravel Blade Check if Array Key Exists Example Tutorial

Reading Time: 4 minutes
113 Views

Inside this article we will see the concept i.e Laravel Blade Check if Array Key Exists Example Tutorial. Article contains the classified information about How to check if array key exists in laravel or not.

If you are looking for a solution i.e Check if array key exists in laravel blade or not 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.

Read More: Laravel Blade Check If Variable Exists or Not Example

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: Check If Array Key Exists in Blade

To check if array key exists or not 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()
    {
        $data = [
            "username" => "sanjay_owt",
            "email" => "sanjay_sample@example.net"
        ];

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

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

Read More: Laravel 9 How To Update Multiple Records Example Tutorial

Code of blade view file:

<!DOCTYPE html>
<html>
<head>
  <title>Laravel Blade Check if Array Key Exists Example Tutorial</title>
</head>
<body>
    
@isset($data['username'])
    <p>Username Key Exists</p>
@endisset
    
</body>
</html>
    

Here, the above code checking $data[‘username’], username key exists inside $data array or not.

Also, in any case if you need to check key within any multidimensional inside array then also you can do that.

Example #2: Check If Array Key Exists in Blade

To check if array key exists or not in laravel blade or not, we will use @isset() 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()
    {
        $data = [
            "user" => [
               "username" => "sanjay_owt",
               "email" => "sanjay_sample@example.net",
               "phone" => "1234567890"
            ]
        ];

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

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

Read More: Laravel 9 Language Translation Methods Example Tutorial

Code of blade view file:

<!DOCTYPE html>
<html>
<head>
  <title>Laravel Blade Check if Array Key Exists Example Tutorial</title>
</head>
<body>
    
@isset($data['user']['email'])
    <p>Email Key Exists</p>
@endisset
    
</body>
</html>
    

Here, the above code checking $data[‘user’][’email’], email key exists within user array inside $data array or not.

We hope this article helped you to learn Laravel Blade Check if Array Key Exists Example Tutorial in a very detailed way.

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.