How to Access Array in blade laravel

Created at 11-Aug-2021 , By samar

How to Access Array in blade laravel

We’ll attempt to use programming in this lesson to solve the "How to Access Array in blade laravel" puzzle.

You can access the array in the laravel blade using ['key']. Here are some examples like {{ $user['name'] }} and {{ $users[0]['name'] }}.
  • Display array value for single record in laravel

    //routes\web.php
    //Get single array value from eloquent collection using toArray() method on first()  
    Route::get('/access-array-value', function(){
        $user = App\Models\User::first()->toArray();
        return view('array', compact('user'));
    });
    
    //resources\views\<array>.blade.php
    //Access array value for single record
    {{  $user['name'] }}
    
  • Display array value of multiple records in laravel

    //routes\web.php
    Route::get('/access-array-value', function(){
        $users = App\Models\User::get()->toArray();
        return view('array', compact('users'));
    });
    
    //resources\views\<array>.blade.php
    @foreach($users as $user)
        {{  $user['name'] }} <br/>
    @endforeach
    
    //Access array value without foreach using key. You can change the key from 0 to 1,2,3,.... to access the array value.
    {{ $users[0]['name'] }}
    

Back to code snippet queries related laravel

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.