我想更新一个JSON我的数据库中的列,但我收到此错误:
Array to string conversion
我已将列名称声明为array
在模型中:
protected $casts = [
'destinations' => 'array'
];
这是我使用的代码:
$data[] = [
'from' => $fromArray,
'to' => $toArray
];
Flight::where('id', $id)->update(['destinations' => $data]);
我应该怎么办 ?
您可以使用箭头访问您的 json 键,以便您可以像这样更新您的列:
Flight::where('id', $id)->update([
'destinations->from' => $data['from'],
'destinations->to' => $data['to']
]);
正如@fubar提到的,你必须有mysql
5.7 https://dev.mysql.com/doc/refman/5.7/en/json.html为了让我的解决方案发挥作用。
检查docs https://laravel.com/docs/5.4/queries#updating-json-columns
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)