【发布时间】:2021-09-24 03:28:16
【问题描述】:
我有一个数据库,carpark 和 car 记录由 car_park 数据透视表链接。
我的数据透视表中的一些行 (car_park)
id | carpark_id | car_id | in_date | out_date
1 | 1 | 1 | 2020-01-01 | 2020-01-30
2 | 1 | 2 | 2020-02-01 | 2020-02-28
3 | 1 | 1 | 2020-03-01 | 2020-03-31
....
在我的停车场模型中,我有以下条目:
public function cars() {
return $this->belongsToMany('App\Admin\Cars')->withPivot(['id', 'in_date', 'out_date']);
}
保存我需要更新数据透视表行 id 1,我无法使用
$carpark->$cars()->updateExistingPivot($car_id, $in_date, $out_date );
因为上面的语句会同时更新第 1 行和第 3 行。
如何使用数据透视表行 ID 更新数据透视表?
【问题讨论】:
标签: php laravel pivot-table