[英]relationships with multiple pivot table Eloquent/Laravel
I have 4 table我有 4 张桌子
users
id
name
school
id
name
user_school
id
user_id
school_id
user_role
id
user_role
role_id
We need to find school users list whose have role id is 2我们需要找到角色 id 为 2 的学校用户列表
we use belongsToMany but not success我们使用 belongsToMany 但没有成功
public function totalAdmin(){
return $this->belongsToMany('App\Models\User','user_school', 'school_id','user_id')->where('user_school.status',1)->where('user_school.deleted',0);
}
Assuming you have relationships:假设你有关系:
roles
(with pivot user_role)roles
(使用 pivot user_role)users
(with pivot user_school) users
(使用 pivot user_school) You can do:你可以做:
School::users()->whereHas('roles', function ($q) {
$q->where('id', 2);
});
You can probably transfer it in the relationship as well:您也可以在关系中转移它:
public function totalAdmin(){
return $this->belongsToMany('App\Models\User','user_school','school_id','user_id')
->whereHas('roles', function($q) { $q->where('id', 2); });
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.