title: Laravel Login and redirect depending on role id
body: <p>So I have login page that the admin or users can use to login. It went well until such time we have some changes and added the admin side. I only uses 1 table for all user types and I have <code>role_id</code> column that defines the users role. So if the <code>role_id</code> is <code>0</code>, I have to redirect them to the dashboard page whereas if it's a user, will redirect to user page. I have tried as what is suggested in the internet but can't make it work. Here's what I have:</p>
  
  <pre><code>class UserLoginController extends Controller
  {
      /**
       * Create a new controller instance.
       *
       * @return void
       */
      public function __construct()
      {
          $this-&gt;middleware('guest:user')-&gt;except('logout');
      }
  
      public function showLoginForm()
      {
          return view('auth.user-login');
      }
  
      public function login(UserLoginRequest $request)
      {
          // Attempt to log the user in
          if (Auth::guard('user')-&gt;attempt(['email' =&gt; $request-&gt;email, 'password' =&gt; $request-&gt;password])) {
              // if successful, then redirect to their intended location
              return redirect()-&gt;intended(route('user.dashboard'));
          }
  
          // if unsuccessful, then redirect back to the login with the form data
          if (! User::where('email', $request-&gt;email)-&gt;where('password', bcrypt($request-&gt;password))-&gt;first() ) {
              return redirect()-&gt;back()
                  -&gt;withInput($request-&gt;only('email'))
                  -&gt;withErrors(['status' =&gt;  'Incorrect username or password.']);
          }
      }
      public function logout()
      {
          Auth::guard('user')-&gt;logout();
          return redirect()-&gt;route('user.login');
      }
  }
  </code></pre>
  
  <p><strong>config/auth</strong></p>
  
  <pre><code>'guards' =&gt; [
      'web' =&gt; [
          'driver' =&gt; 'session',
          'provider' =&gt; 'users',
      ],
  
      'api' =&gt; [
          'driver' =&gt; 'token',
          'provider' =&gt; 'users',
          'hash' =&gt; false,
      ],
  
      'guest' =&gt; [
          'driver' =&gt; 'session',
          'provider' =&gt; 'guests',
      ],
  
      'user' =&gt; [
          'driver' =&gt; 'session',
          'provider' =&gt; 'users',
      ],
  ],
  
  'providers' =&gt; [
      'users' =&gt; [
          'driver' =&gt; 'eloquent',
          'model' =&gt; App\User::class,
      ],
  
      'guests' =&gt; [
          'driver' =&gt; 'eloquent',
          'model' =&gt; App\Guest::class,
      ],
  
  ],
  </code></pre>
  
  <p><strong>MODEL</strong></p>
  
  <pre><code>&lt;?php
  
  namespace App\Models;
  
  use Illuminate\Foundation\Auth\User as Authenticatable;
  
  class User extends Authenticatable
  {
      protected $guard = 'user';
  
      protected $fillable = [
          //fillable cols
      ];
  
      //change role id data type to integer
      protected $casts = [
          'role_id' =&gt; 'integer',
      ];
  }
  </code></pre>
  
  <p>Now, where should I insert the condition to check if it's an admin or a user? </p>