Search code examples
phparrayslaravelarrayobject

Fetch value from array saved in a single column in Laravel


I would like to get a single value from an array which is has been saved in a single mysql column in Laravel.

My mysql table columns look like this from the Controller:

$registrations = RegistrationLog::groupBy('phone_number')->get();

enter image description here

I would like to get into the request object section and get the email address value from the that array which is saved in the request_object column.

Please let me know how I can achieve this in laravel because I have tried using the below code inside a foreach in the blade but throws a 'Trying to get property of non-object error'

The code:

@foreach($registrations as $reg)
    <tr>
        <td>
            {!! $reg->request_object->email !!}
        </td>   
    </tr>
@endforeach

Solution

  • You can use mutation, to format eloquent property like you want befor access. Also you can use attribute casting which will be applied when you call ->toArray() method to Eloquent model. Or just json_decode($reg->request_object)->email