我有很多用户和角色结构
角色
ID
名称
ROLE_USER
用户身份
ROLE_ID
模型
user.PHP的
public function roles() { return $this->belongsToMany('Role'); }
Role.PHP
public function users() { return $this->belongsToMany('User'); }
解决方法
@H_404_40@ 这应该为您提供所有管理员用户.$users = User::whereHas('roles',function($q) { $q->where('name','=','admins'); })->get();
您可以在http://laravel.com/docs/eloquent#querying-relations查看有关has()方法的更多信息