Laravel casting JSON to array?
Just use json_decode
with option array true
json_decode($casts['content'], true));
If you want your mind blown...
// automatically handles json_encode, json_decode to php object
protected $casts = [
'db_json_column' => 'object'
];
$model->db_json_column = $array; // persisted as json
$object = $model->db_json_column; // retrieved as object
or,
// automatically handles json_encode, json_decode to php array
protected $casts = [
'db_json_column' => 'array'
];
$model->db_json_column = $array; // persisted as json
$array = $model->db_json_column; // retrieved as array
When you define array
cast you don't need to do any json_encode
or json_decode
. When you want to insert, simple you need to do:
'content'=> [
'description' => $faker->paragraph(3),
'about' => $faker->paragraph(2),
'info' => $faker->paragraph(2),
'updated' => $faker->dateTimeBetween('-1 years', 'now')
],
Laravel will do the rest
And when you want to get content
field data, you simple need to use:
$campaign->content;
and you will have here array, so if you want to display description, you simple need to do:
echo $campaign->content['description'];