gpt4 book ai didi

php - MySQL 中的双引号 (Laravel Seeder)

转载 作者:行者123 更新时间:2023-11-28 23:21:49 27 4
gpt4 key购买 nike

我在使用 Laravel DB Seeder 时遇到了问题。我试图为 json 行赋值:

'rating' => '{"avg":2,"clean":2,"place":1,"keeper":1}'

我在 MySQL 中得到了这个:

"{\"avg\":2,\"clean\":2,\"place\":1,\"keeper\":1}"

但是创建json数组没有问题:

[{"number":"+7 999 9999999","name":"Some name"},{"number":"8 111 1234567","name":"Body"}]

对不起我的英语。请帮忙!

更新:完整的播种器代码

class ObjectTableSeeder extends Seeder {

public function run()
{
\App\Object::create([
'category_id' => '2',
'owner_id' => '1',
'name' => 'Some name"',
'rating' => '{"avg":2,"clean":2,"place":1,"keeper":1}',
'phones' => '[{"number":"+7 999 9999999","name":"Some name"},{"number":"8 111 1234567","name":"Body"}]',
'status' => '1'
]);
}
}

最佳答案

简单的改变

'rating' => '{"avg":2,"clean":2,"place":1,"keeper":1}'

像这样排列

'rating' => ["avg" => 2, "clean" => 2, "place" => 1, "keeper" => 1]

然后在 Object 模型中将该字段转换为这样的数组

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

因此,无论何时你将评分保存到数据库中,laravel 都会对其进行序列化,并在检索时将其反序列化为一个数组

关于php - MySQL 中的双引号 (Laravel Seeder),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41250881/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com