mardi 9 octobre 2018

Eloquent Casting to Array not always working

I have the following code that sets a value inside a JSON-type column

auth()->user()->update([
    'address' => [
        'street' => $address->getStreet(),
        'city'   => $address->getCity(),
    ],
]);

This results in the following data inside the table column

{"street":"Korte Tiendeweg","city":"Gouda"}

In the Model I cast it as an array:

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

But when I call the Model and dump the data (via return auth()->user();), it shows the supposed to be json as a strange unparsed JSON: address: "{\"street\":\"Korte Tiendeweg\",\"city\":\"Gouda\"}",

What did I miss?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire