Get all users with role in Laravel

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Effectively Retrieve Users with Specified Roles in Laravel Introduction In Laravel, managing roles and relationships between users and their assigned roles can be done efficiently using the framework's built-in relationship features. However, sometimes, developers may struggle to get all users that have a particular role, such as 'Teacher'. In this post, we will discuss methods to achieve this goal. 1. Using Eloquent Relationships with Where Clauses in Query Builder The first approach involves using the Laravel's query builder, which allows us to modify the relationship query by adding where clauses on specific columns. With this method, you can fetch all users that have a particular role (e.g., teacher) and their related information. Here is an example:
$students = User::with(array('Roles' => function($query) {
        $query->where('name','Teacher');
    }))
    ->get();

return $students;
A closer look at this code shows that we are using the with() method to load the related roles for each user. The function passed into the with() method executes a new query on the relationships. Within this custom query, we apply a where clause to filter only the users that have the role 'Teacher'. 2. Using Eloquent Relationships with BelongsToMany Relations Another approach is to use Laravel's belongsToMany() relationship, which defines a many-to-many relationship between two models. This can be useful when you want to get all users with a specific role without the help of the query builder:
$students = User::with('Roles')
    ->whereHas('roles', function($query) {
        $query->where('name','Teacher');
    })
    ->get();

return $students;
In this case, we are loading the related roles for each user in the query. Then, we use whereHas() to define a condition that returns true when at least one of the associated relations (in this case, the role) satisfies the given criteria (only if it has the 'Teacher' role). 3. Using Eloquent Relationships with HasRole Trait You can also use Laravel's ConfideUser and EntrustRole models to manage roles. To retrieve all users with a particular role, you can utilize the built-in functionality provided by these two classes:
$students = User::with('Roles')
    ->join('assigned_roles', 'users.id', '=', 'assigned_roles.user_id')
    ->where('roles.name', 'Teacher')
    ->get();

return $students;
In this example, we are loading the roles for each user and then joining the assigned_roles table, matching on the user's ID and role name (where 'users.id' matches 'assigned_roles.user_id' and 'roles.name' is equal to 'Teacher'). This query will return all users that are associated with the 'Teacher' role. Conclusion In summary, there are multiple ways to retrieve all users with a specified role in Laravel. You can use Eloquent relationships and their flexible methods such as has() or whereHas() to filter users based on their roles. Additionally, you may leverage the built-in functionality provided by models like ConfideUser and EntrustRole. Whichever approach you choose, make sure it aligns with your project's architecture and follows the Laravel best practices for efficient code.