我想更新数据库中的 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']
]);
As @fubar mentioned you have to have mysql 5.7 in order to have my solution to work.
检查docs
关于php - Laravel Eloquent 不更新 JSON 列 : Array to string conversion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45361861/