Laravel 将 JSON 转换为数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34481016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:54:07  来源:igfitidea点击:

Laravel casting JSON to array?

phpjsonlaravellaravel-5eloquent

提问by jycr753

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

我正在尝试将 JSON 字段转换为数组。例如模型是这样的:

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?

任何想法为什么?

回答by Marcin Nabia?ek

When you define arraycast you don't need to do any json_encodeor json_decode. When you want to insert, simple you need to do:

当您定义array演员表时,您无需执行任何json_encodejson_decode. 当你想插入时,你需要做的很简单:

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

Laravel will do the rest

Laravel 将完成剩下的工作

And when you want to get contentfield data, you simple need to use:

当您想获取content字段数据时,您只需使用:

$campaign->content;

and you will have here array, so if you want to display description, you simple need to do:

并且您将在此处拥有数组,因此如果您想显示描述,您只需执行以下操作:

echo $campaign->content['description'];

回答by zt1983811

Just use json_decodewith option array true

只需使用json_decodeoption array true

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

回答by mangonights

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