【问题标题】:Laravel edit pivot table directlyLaravel 直接编辑数据透视表
【发布时间】:2021-09-24 03:28:16
【问题描述】:

我有一个数据库,carparkcar 记录由 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


【解决方案1】:

使用wherePivot 查找数据透视表ID

$carpark->cars()->wherePivot('id',1)->update(['in_date'=>'','out_date '=>'']);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 2017-12-15
    • 2021-08-17
    • 1970-01-01
    相关资源
    最近更新 更多