Laravel 7.0 版
我有Team
模型和User
模型,team_has_users
表。
team_has_users
表有team_id
, user_id
,role
列。
一个用户可以属于一个具有不同角色的团队。
例如,一个用户可以作为客户和员工属于一个团队。
在Team
模型中,我设置了这样的关系。
public function users(){
return $this->belongsToMany(User::class, 'team_has_user', 'team_id', 'user_id')
->withPivot('role');
}
当我将用户附加到团队时,它就像这样运行良好。
$item->users()->attach($request->clients, ['role'=>'client']);
$item->users()->attach($request->employees, ['role'=>'employee']);
但是,当我要同步它们时,我做不到。
我试图搜索并找到一个类似的,syncwithoutDetaching
但它似乎不适合我的情况。
team_has_users
表可以是这样的。
team_id user_id role
1 1 client
1 1 employee
1 2 client
1 1 other
...
谁能帮我?
谢谢!