Laravel Arr has() function Example

In this example, I explain Laravel Arr has() function. Laravel Arr::has() method checks specific item or items exists in an array using dot notation. If item or items exists in an array then laravel Arr has() function return true. Otherwise return false.

Laravel Arr::has() method is most important when you check specific key or element exists in an array in your laravel application.

Read Also :  Laravel Create Directory If Doesn't Exists Example

Example : Laravel Arr::has() method

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Arr;

class UserController extends Controller
{
    public function index(){        
        $array = ['product' => ['name' => 'Desk', 'price' => 100]];

        if (Arr::has($array, 'product.name')){
            echo 'Item is exists';
        }else{
            echo 'Item is not exists';
        }

        echo "<br>";
        // true

        if(Arr::has($array, ['product.price', 'product.discount'])){
            echo 'Item is exists';
        }else{
            echo 'Item is not exists';
        }

        // false
    }
}

routes/web.php

<?php

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

Route::get('users', [UserController::class,'index']);

I hope you understand laravel Arr::has() method and it can help you…