我有多对多的用户和角色结构
users
id
name
roles
id
name
角色_用户
user_id
role_id
Model
User.php
public function roles() {
return $this->belongsToMany('Role');
}
Role.php
public function users() {
return $this->belongsToMany('User');
}
有两个数据admins
and members
在角色表中,我想知道要过滤哪个角色是管理员的用户。
这应该为您提供所有管理员用户。
$users = User::whereHas('roles', function($q) {
$q->where('name', '=', 'admins');
})->get();
您可以查看有关的更多信息has()
方法在http://laravel.com/docs/eloquent#querying-relations
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)