title: How to select users based on the role that they have in Laravel 5?
body: <p>From my controller I would like to select all of the users that have the role "client".</p>
  
  <p>I have a User model and a Role model. A role belongs to many users and a user belongs to many roles. </p>
  
  <p>I've established my models and have some helper functions at the model instance level for fetching roles and users</p>
  
  <p>Here are the User and Role database models:</p>
  
  <p><strong>app/User.php</strong></p>
  
  <pre><code>class User extends Authenticatable
  {
      use Notifiable;
  
      protected $fillable = [
          'name', 'email', 'password',
      ];
  
      protected $hidden = [
          'password', 'remember_token',
      ];
  
      // User belongs to many roles
      public function roles()
      {
          return $this-&gt;belongsToMany('App\Role')-&gt;withTimestamps();
      }
  
      // whitelist specific roles
      // reject if user does not have given roles
      public function authorizeRoles($roles)
      {
          if ($this-&gt;hasAnyRole($roles)) {
              return true;
          }
  
          abort(401, 'This action is unauthorized.');
      }
  
      // Check if a user has a role
      public function hasRole($role)
      {
        if ($this-&gt;roles()-&gt;where('name', $role)-&gt;first())
        {
          return true;
        }
  
        return false;
      }
  
      // Pass in string or array to check if the user has a role
      public function hasAnyRole($roles)
      {
        if (is_array($roles)) {
          foreach ($roles as $role) {
            if ($this-&gt;hasRole($role)) {
              return true;
            }
          }
        } else {
          if ($this-&gt;hasRole($roles)) {
            return true;
          }
        }
        return false;
      }
  }
  </code></pre>
  
  <p><strong>app/Role.php:</strong></p>
  
  <pre><code>class Role extends Model
  {
      public function users()
      {
          return $this-&gt;belongsToMany('App\User')-&gt;withTimestamps();
      }
  }
  </code></pre>
  
  <p>I have migrations for a <strong>create_users_table</strong>, <strong>create_roles_table</strong> and a pivot table for <strong>create_role_user_table</strong>. Each role has an id, name and description. Each user has an id, name, email and password.</p>
  
  <p>I would like to filter by all users that have the role called "client".</p>
  
  <p>In my controller method I try to call roles but it does not work because it is an instance method:</p>
  
  <pre><code>// Display admin dashboard
  public function admin(Request $request)
  {
      // check to make sure user is an admin
      $request-&gt;user()-&gt;authorizeRoles('admin');
  
      // Display all users that have the role "client"
      // ***** section that does not work ********
      $users = User::all()-&gt;roles()-&gt;where('name', 'client')-&gt;get();
  
      return view('admin', compact('users'));
  
  }
  </code></pre>
  
  <p>How can I populate the <code>$users</code> variable with only users who have the role with name "client"?</p>