Query (list all users) by team session #2648
-
I making an endpoint to query all users which is just Route::get('/list', [UserController::class, 'list'])->middleware(['role:super-admin|admin']);
public function list()
{
$current_user = app('auth')->user();
$result = [];
if ($current_user->hasRole('super-admin')) {
// Super-admins query all users
$result = User::with('teams')->get(); // ✅ this is working good (easy to query all users)
} else if ($current_user->hasRole('admin')) {
// Admins query users in their team
// ❓❓❓❓
}
// Return response
return response()->api($result, 200);
} User model: public function teams()
{
return $this->belongsToMany(Team::class, 'model_has_roles', 'model_id', 'team_id')->withPivot('model_type');
} The question is if I know I can query all users and filter one by one, but I think that is not correct way to do, or bad perf. Thanks for help |
Beta Was this translation helpful? Give feedback.
Answered by
parallels999
Apr 5, 2024
Replies: 1 comment 2 replies
-
by using |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
l2aelba
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
by using
setPermissionsTeamId($your_team_id)
before the query