Laravel雄辩不会更新JSON列:数组到字符串的转换 [英] Laravel eloquent does not update JSON column : Array to string conversion

查看:73
本文介绍了Laravel雄辩不会更新JSON列:数组到字符串的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新数据库中的 JSON 列,但出现此错误:

I want to update a JSON column in my database but I get this error :

Array to string conversion  

我在模型中将列名称声明为array:

I have declared the column name as array in the model :

protected $casts = [
    'destinations' => 'array'
];

这是我使用的代码:

$data[] = [
    'from' => $fromArray,
    'to' => $toArray
];

Flight::where('id', $id)->update(['destinations' => $data]);

我该怎么办?

推荐答案

根据Github上的对话: Taylor Otwell推荐使用save方法:

According to this conversation on Github : Make json attributes fillable if Model field is fillable Taylor Otwell recommande the use of save method :

$ model-> options = ['foo'=> 'bar'];

$model->options = ['foo' => 'bar'];

$ model-> save();

$model->save();

因此,在这种情况下,您可以这样操作:

So in you case you can do it like this :

$flight = Flight::find($id); 
$flight->destinations = $data; 
$flight->save();

这篇关于Laravel雄辩不会更新JSON列:数组到字符串的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆