Laravel : 如何获取具有某个角色的所有用户?
Laravel : How to get all users who have a certain role?
我有三个角色:1.Admin
2.Client
3.Store
我有三个表:1.users
2.roles
3.role_user
如何获取所有具有 Client
角色的用户?
我试过了
$clients = User::roles()->where('App\Models\Role',Role::CLIENT)->get();
我遇到以下错误。
Non-static method App\Models\User::roles() should not be called
statically
榜样
class Role extends Model
{
public const ADMIN = 'Admin';
public const CLIENT = 'Client';
public const STORE = 'Store';
public function users()
{
return $this->belongsToMany('App\Models\User')->using('App\Models\UserRole');
}
}
用户模型
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name',
'first_name',
'last_name',
'email',
'password',
'activated',
'token',
'signup_ip_address',
'signup_confirmation_ip_address',
'signup_sm_ip_address',
'admin_ip_address',
'updated_ip_address',
'deleted_ip_address',
];
protected $hidden = [
'password', 'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
public function hasRole(String $roleName)
{
return $this->roles()->where('name', $roleName)->exists();
}
public function roles()
{
return $this->belongsToMany('App\Models\Role');
}
public function addRole(String $roleName)
{
$role = Role::where('name', $roleName)->first();
if ($role) $this->roles()->save($role);
}
}
你可以用whereHas()
方法来完成。这是一种在查询
中使用 exists
以关系为条件的方法
$clients = User::whereHas('roles', function($role) {
$role->where('name', '=', Role::CLIENT);
})->get();
如果你也想得到角色,就堆叠with()
方法
$clients = User::whereHas('roles', function($role) {
$role->where('name', '=', Role::CLIENT);
})->with(['roles' => function($role) {
$role->where('name', '=', Role::CLIENT);
}])->get();
那是因为您试图在模型 class 而不是实例上调用 roles
方法,它应该是这样的
$clients = Role::whereName('client')->first()->users;
我有三个角色:1.Admin
2.Client
3.Store
我有三个表:1.users
2.roles
3.role_user
如何获取所有具有 Client
角色的用户?
我试过了
$clients = User::roles()->where('App\Models\Role',Role::CLIENT)->get();
我遇到以下错误。
Non-static method App\Models\User::roles() should not be called statically
榜样
class Role extends Model
{
public const ADMIN = 'Admin';
public const CLIENT = 'Client';
public const STORE = 'Store';
public function users()
{
return $this->belongsToMany('App\Models\User')->using('App\Models\UserRole');
}
}
用户模型
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name',
'first_name',
'last_name',
'email',
'password',
'activated',
'token',
'signup_ip_address',
'signup_confirmation_ip_address',
'signup_sm_ip_address',
'admin_ip_address',
'updated_ip_address',
'deleted_ip_address',
];
protected $hidden = [
'password', 'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
public function hasRole(String $roleName)
{
return $this->roles()->where('name', $roleName)->exists();
}
public function roles()
{
return $this->belongsToMany('App\Models\Role');
}
public function addRole(String $roleName)
{
$role = Role::where('name', $roleName)->first();
if ($role) $this->roles()->save($role);
}
}
你可以用whereHas()
方法来完成。这是一种在查询
exists
以关系为条件的方法
$clients = User::whereHas('roles', function($role) {
$role->where('name', '=', Role::CLIENT);
})->get();
如果你也想得到角色,就堆叠with()
方法
$clients = User::whereHas('roles', function($role) {
$role->where('name', '=', Role::CLIENT);
})->with(['roles' => function($role) {
$role->where('name', '=', Role::CLIENT);
}])->get();
那是因为您试图在模型 class 而不是实例上调用 roles
方法,它应该是这样的
$clients = Role::whereName('client')->first()->users;