laravel - Laravel / Eloquent - 如何在 updateExistingPivot() 调用中添加额外的 wherePivot() 条件?
问题描述
我有一个包含以下列的参考表:
-user_id (foreign key)
-location_id (foreign key)
-skill_level
-court_id
我可以更新skill_level
上表中的数据透视列值:
$user = User::find($user_id)
$user->locations()->updateExistingPivot($location_id, array(
'skill_level' => $new_value
));
我想添加类似于wherePivot()
上述内容的内容,以便仅更新与某个court_id
(除了user_id
and之外location_id
)匹配的记录。类似于:
->wherePivot('court_id', '=', $court_id);
我将如何通过updateExistingPivot()
电话做到这一点?
解决方案
我最终使用了newPivotStatement()
- 它允许您从数据透视表中启动一系列语句。
而不是 using updateExistingPivot()
,调用看起来像:
$user->locations()->newPivotStatement()->where('location_id', '=', $location_id)
->where('court_id', '=', $court_id)->update(array(
'skill_level' => $new_value
));
推荐阅读
- database - 制表符分隔的关系 PostgreSQL 数据库
- c++ - C ++中自定义默认构造函数和默认默认构造函数有什么区别
- bash - 动态 bash 关联数组键
- css - 有没有办法使用纯 CSS 或其他方式将非硬编码背景混合到另一个非硬编码背景中?
- python - 对于这个与树相关的问题,我该怎么办?
- python - 如何从 Python 中的函数返回结果
- vba - 使用 ComboBox 中的值格式化未绑定的文本框
- android - 有没有办法在应用安装(Android 或 iOS)上跟踪检测,如果他们已经安装或从未安装过应用?
- java - 为什么我不能在使用scanner.close() 后创建另一个scanner 对象?
- django - Django:如果未提供 CharField 和 TextField,则不会引发错误