I'm developing a site for a project, and on my users screen, I've implemented a filter for access levels. On my screen I have a select with a value 'author' and another value 'admin'.
Only the 'user' and 'role' relationship is a Many to Many relationship.
What I'm doing is grabbing all users and then foreach to unset the authors or admins, depending on the value quit in $ _GET ['access'].
$users = $users->orderBy('name')->paginate($this->pageSize)->appends([
'acesso' => $request->acesso,
]);
if ($request->has('acesso') && $request->acesso != null){
$request->acesso == 'admin' ? $unsetIfHasAdminRole = false : $unsetIfHasAdminRole = true;
foreach ($users as $key => $user){
if ($user->hasTheRole('admin') == $unsetIfHasAdminRole){
unset($users[$key]);
}
}
}
The hasTheRole method receives a string and returns a boolean indicating whether the user has that.
I wonder if there is any Eloquent method that automates this?
Something like: $users = User::where($user->hasTheRole('admin'))->get();