title: Laravel - Custom authentification query
body: <p>How can I add <code>DESC</code> to the default login sql query? </p>
  
  <p>I mean on default is something like </p>
  
  <p><code>select * from users where name = user_name limit 1</code>. </p>
  
  <p>How can I add </p>
  
  <p><code>select * from users where name = user_name ORDER BY id DESC limit 1</code>?</p>
  
  <p>I know that the name column should contain unique values only, my login system it's different (some predefined users in another table) and I need multiple user registrations with the same name. I just want to login on the last record in the database. Please help me how can I customize the model provider in laravel? I don't know what files to modify for this to work.</p>
  
  <p>This is my LoginController.php but you can ignore it (I added it because some users required it) just look at the default loginController from <code>php artisan make:auth</code></p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers\Auth;
  
  use App\Http\Controllers\Controller;
  use Illuminate\Foundation\Auth\AuthenticatesUsers;
  use Illuminate\Http\Request;
  use Illuminate\Validation\ValidationException;
  use Illuminate\Support\Facades\Session;
  class LoginController extends Controller
  {
      /*
      |--------------------------------------------------------------------------
      | Login Controller
      |--------------------------------------------------------------------------
      |
      | This controller handles authenticating users for the application and
      | redirecting them to your home screen. The controller uses a trait
      | to conveniently provide its functionality to your applications.
      |
      */
  
      use AuthenticatesUsers;
  
      /**
       * Where to redirect users after login.
       *
       * @var string
       */
      protected $redirectTo = '/home';
  
      /**
       * Create a new controller instance.
       *
       * @return void
       */
      public function __construct()
      {
          $this-&gt;middleware('guest')-&gt;except('logout');
      }
  /**
       * Check either username or email.
       * @return string
       */
  
  public function login(Request $request)
      {
          $this-&gt;validateLogin($request);
  
          // If the class is using the ThrottlesLogins trait, we can automatically throttle
          // the login attempts for this application. We'll key this by the username and
          // the IP address of the client making these requests into this application.
          if ($this-&gt;hasTooManyLoginAttempts($request)) {
              $this-&gt;fireLockoutEvent($request);
  
              return $this-&gt;sendLockoutResponse($request);
          }
  
          if ($this-&gt;attemptLogin($request)) {
              return $this-&gt;sendLoginResponse($request);
          }
  
          // If the login attempt was unsuccessful we will increment the number of attempts
          // to login and redirect the user back to the login form. Of course, when this
          // user surpasses their maximum number of attempts they will get locked out.
          $this-&gt;incrementLoginAttempts($request);
  
          return $this-&gt;sendFailedLoginResponse($request);
      }
  
        public function username()
      {
          $identity  = Session::get('table_id');
          $fieldName = 'name';
          request()-&gt;merge([$fieldName =&gt; $identity]);
  
          return $fieldName;
      }
  
      /**
       * Validate the user login.
       * @param Request $request
       */
      protected function validateLogin(Request $request)
      {
          $this-&gt;validate(
              $request,
              [
                  'password' =&gt; 'required|string',
              ],
              [
                  'password.required' =&gt; 'Password is required',
              ]
          );
      }
      /**
       * @param Request $request
       * @throws ValidationException
       */
      protected function sendFailedLoginResponse(Request $request)
      {
          $request-&gt;session()-&gt;put('login_error', trans('auth.failed'));
          throw ValidationException::withMessages(
              [
                  'error' =&gt; [trans('auth.failed')],
              ]
          );
      }
  
       protected function attemptLogin(Request $request)
      {
          $remember = true;
          return $this-&gt;guard()-&gt;attempt(         
              $this-&gt;credentials($request), $remember
          );
      }
  }
  </code></pre>
  
  <p>All methods in my LoginController overrides methods from <code>vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php</code></p>