Laravel / Eloquent - 如何向 updateExistingPivot() 调用添加额外的 wherePivot() 条件?
Laravel / Eloquent - How Do You Add An Additional wherePivot() Condition To An updateExistingPivot() Call?
我有一个参考 table 包含以下列:
-user_id (foreign key)
-location_id (foreign key)
-skill_level
-court_id
我可以更新上面 table 中的 skill_level
数据透视列值:
$user = User::find($user_id)
$user->locations()->updateExistingPivot($location_id, array(
'skill_level' => $new_value
));
我想在上面添加类似于 wherePivot()
的内容,以便只有匹配特定 court_id
的记录(除了 user_id
和 location_id
) 更新。类似于:
->wherePivot('court_id', '=', $court_id);
如何使用 updateExistingPivot()
调用来完成此操作?
我最终使用了 newPivotStatement()
- 它允许您从主元 table.
开始一系列语句
而不是使用 updateExistingPivot()
,调用看起来像:
$user->locations()->newPivotStatement()->where('location_id', '=', $location_id)
->where('court_id', '=', $court_id)->update(array(
'skill_level' => $new_value
));
我有一个参考 table 包含以下列:
-user_id (foreign key)
-location_id (foreign key)
-skill_level
-court_id
我可以更新上面 table 中的 skill_level
数据透视列值:
$user = User::find($user_id)
$user->locations()->updateExistingPivot($location_id, array(
'skill_level' => $new_value
));
我想在上面添加类似于 wherePivot()
的内容,以便只有匹配特定 court_id
的记录(除了 user_id
和 location_id
) 更新。类似于:
->wherePivot('court_id', '=', $court_id);
如何使用 updateExistingPivot()
调用来完成此操作?
我最终使用了 newPivotStatement()
- 它允许您从主元 table.
而不是使用 updateExistingPivot()
,调用看起来像:
$user->locations()->newPivotStatement()->where('location_id', '=', $location_id)
->where('court_id', '=', $court_id)->update(array(
'skill_level' => $new_value
));