我在使用Laravel DB Seeder时遇到了麻烦。我试图把值放到json行:
'rating'=> '{ “平均”:2, “干净”:2, “地点”:1, “员”: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'
]);
}
}
答案 0 :(得分:1)
只需更改
'rating' => '{"avg":2,"clean":2,"place":1,"keeper":1}'
像这样的数组
'rating' => ["avg" => 2, "clean" => 2, "place" => 1, "keeper" => 1]
然后在对象模型中将该字段转换为像这样的数组
protected $casts = [
'rating' => 'array',
];
因此,只要您将评级保存到数据库laravel将序列化它,并在检索它时将其反序列化为数组