Laravel updateOrCreate 忽略了一些字段
Laravel updateOrCreate ignoring some fields
我已经纠结了几个小时试图找出这行代码的问题:
ContentSetting::updateOrCreate(
['content_id' => $this->id, 'key' => 'max_width'],
['value' => $value]
);
它应该检查我的 content_settings table 中是否有具有特定内容 ID 和键“max_width”的行,如果有则更新它,如果没有则更新它创造它。它用于模型修改器,因此 $this.
无论如何,无论我尝试输入什么值,这总是会导致执行此 mysql 查询(仅使用不同的时间戳):
insert into `content_settings` (`content_id`, `updated_at`, `created_at`) values (2, '2020-10-15 14:07:00', '2020-10-15 14:07:00')
...只是用空键和值创建新行。任何人都可以发现错误吗?或者这是某个地方的错误? “key”是保留字的东西?
您应该在模型的 $fillable 数组中设置要在批量分配中使用的字段:
class ContentSetting extends Model
{
protected $fillable = ['content_id','key','value'];
}
我已经纠结了几个小时试图找出这行代码的问题:
ContentSetting::updateOrCreate(
['content_id' => $this->id, 'key' => 'max_width'],
['value' => $value]
);
它应该检查我的 content_settings table 中是否有具有特定内容 ID 和键“max_width”的行,如果有则更新它,如果没有则更新它创造它。它用于模型修改器,因此 $this.
无论如何,无论我尝试输入什么值,这总是会导致执行此 mysql 查询(仅使用不同的时间戳):
insert into `content_settings` (`content_id`, `updated_at`, `created_at`) values (2, '2020-10-15 14:07:00', '2020-10-15 14:07:00')
...只是用空键和值创建新行。任何人都可以发现错误吗?或者这是某个地方的错误? “key”是保留字的东西?
您应该在模型的 $fillable 数组中设置要在批量分配中使用的字段:
class ContentSetting extends Model
{
protected $fillable = ['content_id','key','value'];
}