Thursday, March 2, 2017

Storing data into json field

I have a table with a JSON column. I added this to my model in order to cast it automatically when I want to read it:

protected $casts = ['data' => 'array'];

If I manually insert this in the database to the json field everything works fine:

{"age": "15", "name": "John Doe"}

I read the model field and it's returned as an array.

I cant figure out how to store data through my app. I tried using this

$model->data = json_encode(['John Doe']);

But when I check the database this is what is stored:

"{\"name\":\"John Doe\"}"

And it's not casted when I want to use it. How can I solve this?

PLUS: Is there some documentation about the json field usage? I coulnd find anything



via Alan

Advertisement