Retrieve Value from Field in Laravel Cast Array

663 views Asked by At

I have a table named settings, in it, there are several columns, id,company_id, key, value.

In my Laravel model for Setting, I have the following cast:

protected $casts = [
    'value' => 'array',
];

But when I go to retrieve data that has been stored, I can't.

For example, I have a record with the following value: "{\"default_remit_address\":\"2395\"}"

And when I go to retrieve the record in a Blade, it does pull it up correctly, but I'm not sure how to grab a specific value from the value field (like default_remit_address).

If I print the return "{{$settings->value}}" directly in the Blade, this is what I get:

{"default_remit_address":"2395"}

So how can I go one level deeper?

1

There are 1 answers

2
Spholt On

As this json object is being cast to an Array, you can just use regular array syntax to access it's contents.

$default_remit_address = Settings::find(1)->value['default_remit_address'];

or in your blade template

{{ $settings->value['default_remit_address'] }}