How to Access Array in blade laravel

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

Answers 2
  • 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'] }}
    
    0
  • 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'] }}
    
    0
  • Back to code snippet queries related laravel