Skip to content

Getting all users with a specific role when using teams #1999

Answered by erikn69
mohammad-azzam-dev asked this question in Q&A
Discussion options

You must be logged in to vote

Make your own relation

// User model
public function roles_all(): BelongsToMany
{
    return $this->morphToMany(
        config('permission.models.role'),
        'model',
        config('permission.table_names.model_has_roles'),
        config('permission.column_names.model_morph_key'),
        PermissionRegistrar::$pivotRole
    );
}
// get all users with a specific role
Users::whereHas('roles_all', fn($query) => $query->whereIn("name", ['role1']))->get();
// or 
Users::whereRelation('roles_all', "name", 'role1')->get();
// Also, you could get all roles, from all teams with this
Users::with('roles_all')->get();

Replies: 3 comments 9 replies

Comment options

You must be logged in to vote
7 replies
@mohammad-azzam-dev
Comment options

@mohammad-azzam-dev
Comment options

@mohammad-azzam-dev
Comment options

@erikn69
Comment options

@mohammad-azzam-dev
Comment options

Answer selected by mohammad-azzam-dev
Comment options

You must be logged in to vote
2 replies
@erikn69
Comment options

@erikn69
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants