20

I'm trying to convert the a JSON field to array. For example the model is like this:

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

While in insert the content inside, I do it like this:

'content'=> json_encode([
                'description' => $faker->paragraph(3),
                'about' => $faker->paragraph(2),
                'info' => $faker->paragraph(2),
                'updated' => $faker->dateTimeBetween('-1 years', 'now')
]),

But while getting the data it prints a string, nothing else.

The migration in this part looks like this:

$campaign->json('content');

Sample of the output:

"content": "{\"description\":\"Ut quas quo odio illo. Voluptates quia fuga itaque sint. Velit sapiente fugit ea ut ducimus sint tempora eligendi. Ea et molestiae consequuntur quibusdam soluta voluptatem.\",\"about\":\"Aut voluptates et iste ut perspiciatis. Esse sunt ullam inventore sit doloremque et quisquam.\",\"info\":\"Corrupti et facere exercitationem consequatur aspernatur quo saepe. Omnis et tempore enim ut. Quia magnam quia enim et eos enim.\",\"updated\":{\"date\":\"2015-11-22 08:25:13.000000\",\"timezone_type\":3,\"timezone\":\"UTC\"}}",

Any ideas why?

2
  • RTM: json_encode and json_decode. Also, you already have an array when you set 'key' => 'value'. What are you trying to achieve? xD Commented Dec 27, 2015 at 13:49
  • I am casting the elements from the model so when the controllers returns JSON it does looks like a JSON object and not a string. That way is easy to manipulate and use :) Commented Dec 27, 2015 at 13:53

3 Answers 3

21

When you define a cast to type array you don't need to do any json_encode or json_decode.

When you want to insert, all 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'];
Sign up to request clarification or add additional context in comments.

1 Comment

thanks that was it.. I totally got lost there, since I was placing an array before like in laravel 4, but it throw an error while seeding, but looks like I needed to read the error better... thanks mate :)
21

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

Comments

2

Just use json_decode with option array true

json_decode($casts['content'], true));

2 Comments

How would you implement that when the data is being fetch via relationships and $data->content; to fetch that part of the model?
You can try $data->toArray()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.