php - Laravel Eloquent 不更新 JSON 列 : Array to string conversion

标签 php mysql json laravel eloquent

我想更新数据库中的 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/

相关文章:

json - json在grails中无法正常工作

json - 在 Swift 中访问嵌套数据

javascript - 如何使用 HTML5 SSE 发送 json_encode 数据

javascript - 无法在聊天应用程序中使用 Ajax 调用从 Controller 返回 html

PHP模型(MySQL)设计问题

php - mysql 更新时如何忽略某一列?

json - 如何快速将这个异构对象与模型映射?

php - 返回复选框的状态,如果未选中则返回 0

php - X 分钟前 Twitter 风格

java - 准备好的语句返回 false 但行已插入?