title: laravel authentication with eloquent driver on roles
body: <p>I am trying to authenticate users in my Laravel application. </p>
  
  <p>I am encountering the following problem:  </p>
  
  <ul>
  <li>using driver <strong>database</strong> in auth.php: I can login using auth::attempt(), and auth::check is working, but I can't validate if the logged in user has a certain role.</li>
  <li>using driver <strong>eloquent</strong> in auth.php: I can login using auth::attempt(), but auth::check is not working. I can however check the role of the logged in user.</li>
  </ul>
  
  <p>edit (question): How can I fix this so that with only one of the drivers, i can do a complete authentication and role check?</p>
  
  <p>Migration tables:</p>
  
  <pre><code>Schema::create('users', function ($table) {
          $table-&gt;increments('id');
          $table-&gt;integer('group_id')-&gt;unsigned();
          $table-&gt;string('name', 64);
          $table-&gt;string('email', 64)-&gt;unique();
          $table-&gt;string('username', 64)-&gt;unique();
          $table-&gt;string('phone', 13);
          $table-&gt;string('address', 64);
          $table-&gt;boolean('isresponsible');
          $table-&gt;string('password', 64);
          $table-&gt;rememberToken()-&gt;nullable();
      });
  Schema::create('roles', function ($table) {
          $table-&gt;increments('id');
          $table-&gt;string('name');
      });
  
  Schema::create('users_roles', function ($table) {
              $table-&gt;integer('user_id')-&gt;unsigned();
              $table-&gt;integer('role_id')-&gt;unsigned();
          }
      );
  Schema::table('users_roles', function($table){
          $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users')-&gt;onDelete('cascade');
          $table-&gt;foreign('role_id')-&gt;references('id')-&gt;on('roles');
      });
  </code></pre>
  
  <p>model class User</p>
  
  <pre><code>&lt;?php
  use Illuminate\Auth\UserTrait;`
  use Illuminate\Auth\UserInterface;`
  use Illuminate\Auth\Reminders\RemindableTrait;
  use Illuminate\Auth\Reminders\RemindableInterface;
  
  class User extends Eloquent implements UserInterface, RemindableInterface {
  
  
  use UserTrait, RemindableTrait;
  
  /**
   * The database table used by the model.
   *
   * @var string
   */
  protected $table = 'users';
  public $timestamps = false;
  
  public static $rules = ['name' =&gt; 'required', 'group_id' =&gt; 'required', 'email' =&gt; 'required', 'phone' =&gt; 'required'];
  protected $fillable = ['name', 'group_id', 'email', 'phone', 'address', 'isresponsible', 'password'];
  
  /**
   * The attributes excluded from the model's JSON form.
   *
   * @var array
   */
  protected $hidden = array('password', 'remember_token');
  
  public function group()
  {
      return $this-&gt;belongsTo('Group');
  }
  
  public function userroles(){
      return $this-&gt;hasMany('Userrole');
  }
  
  public function roles()
  {
      return $this-&gt;belongsToMany('Role', 'users_roles');
  }
  
  public function hasRole($check)
  {
      dd($this-&gt;roles-&gt;toArray());
      return in_array($check, array_fetch($this-&gt;roles-&gt;toArray(), 'name'));
  }
  
  public function setBasicPassword($id){
      $user = User::find($id);
      $user-&gt;password = Hash::make('changeme');
      $user-&gt;save();
  }
  
  public function isValid()
  {
      $validation = Validator::make($this-&gt;attributes, static::$rules);
      if ($validation-&gt;passes()) return true;
      $this-&gt;messages = $validation-&gt;messages();
      return false;
  }
  
  
  /**
   * Get the e-mail address where password reminders are sent.
   *
   * @return string
   */
  public function getReminderEmail()
  {
      // TODO: Implement getReminderEmail() method.
  }
  
  /**
   * Get the unique identifier for the user.
   *
   * @return mixed
   */
  public function getAuthIdentifier()
  {
      return $this-&gt;email;
  }
  
  /**
   * Get the password for the user.
   *
   * @return string
   */
  public function getAuthPassword()
  {
      return $this-&gt;password;
  }
  
  /**
   * Get the token value for the "remember me" session.
   *
   * @return string
   */
  public function getRememberToken()
  {
      return $this-&gt;remember_token;
  }
  
  public function setRememberToken($value)
  {
      $this-&gt;remember_token = $value;
  }
  
  public function getRememberTokenName()
  {
      return 'remember_token';
  }
  }
  </code></pre>
  
  <p>model Class Role
      
  
  <pre><code>class Role extends Eloquent
  {
  
  protected $table = 'roles';
  public $timestamps = false;
  
  public static $rules = ['role_id' =&gt; 'required', 'name' =&gt; 'required'];
  protected $fillable = ['name'];
  
  /**
   * Get users with a certain role
   */
  public function userroles()
  {
      return $this-&gt;belongsToMany('User', 'users_roles');
  }
  }
  </code></pre>
  
  <p>HomeController authentication function</p>
  
  <pre><code> public function authenticate(){
      $rules = array(
          'email'    =&gt; 'required|email',
          'password' =&gt; 'required|alphaNum|min:3'
      );
      $validator = Validator::make(Input::all(), $rules);
      if ($validator-&gt;fails()) {
          return Redirect::to('login')
              -&gt;withErrors($validator)
              -&gt;withInput(Input::except('password'));
      } else {
          $userdata = array(
              'email' =&gt; Input::get('email'),
              'password' =&gt; Input::get('password')
          );
          if (Auth::attempt($userdata, true)) {
              return Redirect::action('HomeController@index');
  
          } else {
              return Redirect::action('HomeController@login')-&gt;withInput();
          }
      }
  }
  </code></pre>
  
  <p>USING THE DATABASE DRIVER<br>
  - auth:attempt() and auth::check are working</p>
  
  <pre><code>$this-&gt;beforeFilter('admin', ['only' =&gt; ['index']]); //filter in controller
  //filter in filters;php
  Route::filter('admin', function()
  {
  if(!Auth::check()) return Redirect::action('HomeController@index');
  if(!Auth::user()-&gt;hasRole('admin')) return View::make('errors.401');
  });
  </code></pre>
  
  <p>This fails with 'Call to undefined method Illuminate\Auth\GenericUser::hasRole()'</p>
  
  <p><strong>EDIT</strong> The <strong>database</strong> driver return a GenericUser Object, and I need my own User object. Don't know where I can change this.</p>
  
  <p>Workaround:I'd rather not use this, ugly code and filters (or views) should not need to do this</p>
  
  <pre><code>Route::filter('admin', function()
  {
      if(!Auth::check()) return Redirect::action('HomeController@index');
      $user = User::find((Auth::user()-&gt;id));
      if(!$user-&gt;hasRole('admin')){ return View::make('errors.401');}
  });
  </code></pre>
  
  <p>USING THE ELOQUENT DRIVER  </p>
  
  <ul>
  <li>auth::attempt() succeeds </li>
  <li>auth::check() fails </li>
  <li>no error on the filter </li>
  </ul>