How to get nested value in Laravel collections

  laravel, php

The collection I retrieved from database is something like:

$data = [
    [
        'first_name' => 'John',
        'last_name' => 'Doe',
        'phone' => [
            'home' => 123,
            'work' => 456,
            'cell' => 789,
        ]
    ],
    ....
];

Now I want to put the data into a table, so in table.blade.php I put something like

@foreach ($data as $row)
    <tr>
        @foreach ($columns as $col)
            <td>{{ $row[$col] }}</td>
        @endforeach
    </tr>
@endforeach

I define the $columns as

$columns = [
    'first_name',
    'last_name',
    'phone.home',  // <== I think it cannot access nested value this way, is there any way to do that?
    'phone.work',
    'phone.cell',
];

Source: Laravel

Leave a Reply