首页 > 解决方案 > 从 laravel 中的数据透视表中删除行

问题描述

我有表格(projects, roles, scopes, shifts, users)和表格(project_role_scope_shift_user)作为枢轴。

我想删除特定的行,例如:

[project_id = 1 , scope_id = 2 , ... ]

但是,分离方法不能正常工作。

$user->roles()->detach(role)      !not working   => removes all rows of that role

$user->roles()->detach(role, ['project_id' => $projectId, 'scope_id' => $scopeId, 'shift_id' => $shiftId]);  => this does not work too, removes all rows of that role

标签: laravel

解决方案


尝试这个 :

$user->roles()->wherePivot(['project_id' => $projectId, 'scope_id' => $scopeId, 'shift_id' => $shiftId])->detach(); 

推荐阅读