I would like to fetch all users which have one role or another. I have tried like this and it doesn't work:
$dentist = Role::where('name', 'dentist')->orWhere('name', 'local_admin')->first()->users()->where('clinic_id', $user->clinic_id)->get();
$dentist = Role::where('name', ['dentist', 'local_admin')->first()->users()->where('clinic_id', $user->clinic_id)->get();
$dentist = Role::whereIn('name', ['dentist', 'local_admin')->first()->users()->where('clinic_id', $user->clinic_id)->get();
Is there a simple solution for this?
You have to define the relationship Role
hasMany users
then you can access users
with with
$dentist = Role::with(['users' => function($query){
$query->where('clinic_id', $user->clinic_id);
})])
->where('name', 'dentist')
->orWhere('name', 'local_admin')
->first();
This would go in you Role model
/**
* Get the users for the role.
*/
public function users()
{
return $this->hasMany(User::class);
}
This would go in your User model
/**
* Get the role the user.
*/
public function role()
{
return $this->belongsTo(Role::class);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.