title: How to use authentication on custom model in Laravel
body: <p>I'm making new custom model for user in laravel. I'm using the default <code>User</code> laravel model for one type of users that I will have, and new <code>Merchant</code> model for other type of users.</p>
  
  <p>I make select option in the <code>register</code> view for chosing which type of user will be registered for better control in the controller.</p>
  
  <pre><code>&lt;select id="user_type" name="user_type"&gt;
    &lt;option value="user"&gt;User&lt;/option&gt;
    &lt;option value="merchant"&gt;Merchant&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>This is my modified the default <code>RegisterController</code> for both type of users:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers\Auth;
  
  use App\User;
  use App\Merchant;
  use Validator;
  use App\Http\Controllers\Controller;
  use Illuminate\Foundation\Auth\RegistersUsers;
  
  use Illuminate\Http\Request;
  use App\Http\Requests;
  use Auth;
  
  class RegisterController extends Controller
  {
      /*
      |--------------------------------------------------------------------------
      | Register Controller
      |--------------------------------------------------------------------------
      |
      | This controller handles the registration of new users as well as their
      | validation and creation. By default this controller uses a trait to
      | provide this functionality without requiring any additional code.
      |
      */
  
      use RegistersUsers;
  
      /**
       * Where to redirect users after login / registration.
       *
       * @var string
       */
      protected $redirectTo = '/index';
  
      /**
       * Create a new controller instance.
       *
       * @return void
       */
      public function __construct()
      {
          $this-&gt;middleware('guest');
      }
  
      /**
       * Get a validator for an incoming registration request.
       *
       * @param  array  $data
       * @return \Illuminate\Contracts\Validation\Validator
       */
      public function register(Request $request)
      {
          $validator = $this-&gt;validator($request-&gt;all());
  
          if ($validator-&gt;fails()) {
              $this-&gt;throwValidationException(
                  $request, $validator
              );
          }
  
          //Add custom code here
          $new_user = $this-&gt;create($request-&gt;all());
          //Add custom code here
  
          Auth::guard($this-&gt;getGuard())-&gt;login($new_user);
  
          return redirect($this-&gt;redirectPath());
      }
  
      protected function validator(array $data)
      {
          if($data['user_type']=='user'){
              return Validator::make($data, [
              'name' =&gt; 'required|max:255',
              'email' =&gt; 'required|email|max:255|unique:users',
              'password' =&gt; 'required|min:6|confirmed',
              'first_name' =&gt; 'required|max:255',
              'last_name' =&gt; 'required|max:255',
              'user_type' =&gt;'required'
          ]);
          }else{
              return Validator::make($data, [
              'name' =&gt; 'required|max:255',
              'email' =&gt; 'required|email|max:255|unique:users',
              'password' =&gt; 'required|min:6|confirmed',
              'first_name' =&gt; 'required|max:255',
              'last_name' =&gt; 'required|max:255',
              'user_type' =&gt;'required'
             ]);
          }
      }
  
      /**
       * Create a new user instance after a valid registration.
       *
       * @param  array  $data
       * @return User
       */
      protected function create(array $data)
      {
          if($data['user_type']=='user'){
              return User::create([
              'name' =&gt; $data['name'],
              'email' =&gt; $data['email'],
              'password' =&gt; bcrypt($data['password']),
              'first_name' =&gt; $data['first_name'],
              'last_name' =&gt; $data['last_name'],
          ]);
         }else{
             return Merchant::create([
              'name' =&gt; $data['name'],
              'email' =&gt; $data['email'],
              'password' =&gt; bcrypt($data['password']),
              'first_name' =&gt; $data['first_name'],
              'last_name' =&gt; $data['last_name'],
          ]);
         }
  
      }
  
  }
  </code></pre>
  
  <p>To mention that <code>register</code> function is not by default in <code>register controller</code>, so I put that function because I already view that solution <a href="https://stackoverflow.com/questions/40978157/how-to-register-more-than-one-type-of-users-and-how-to-make-multi-auth-in-larave">How to register more than one type of users and how to make multi auth in laravel 5.3</a> but I don't know how to modify that.</p>
  
  <p>But the problem now is with the authentication after I submit the registration form. If I register new <code>user</code> it's working fine, but if I register <code>merchant</code> it saved the user in database and after that it doesn't log in the merchant if it doesn't exist any <code>user</code> in the <code>users</code> table. But if it already exist any <code>user</code> in the <code>users</code> after I register the <code>merchant</code> it log in the last <code>user</code> which is created in <code>users</code> table. </p>
  
  <p>So my question is how to modify the authentication for merchant user.</p>
  
  <p>Thank you!</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Register function</p>
  
  <pre><code>public function register(Request $request)
      {
           if($request-&gt;user_type =='user'){
                  $auth = auth()-&gt;guard();
              } else{
                  $auth = auth()-&gt;guard('merchant');
              }
  
              $user = $this-&gt;create($request-&gt;all());
              auth()-&gt;login($user);
  
              return redirect($this-&gt;redirectPath());
  
  
      }
  </code></pre>