title: Laravel - Login with username, email or phone
body: <p>I have implemented login with username or email in my existing project and works fine. I want to extend it to login with username, email or phone. I want a user to login with either username, email or phone number and password.</p>
  
  <p>Here is my code
  App\Http\Controllers\Auth\LoginController</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers\Auth;
  use Socialite;
  
  use Illuminate\Http\Request;
  use App\Http\Controllers\Controller;
  use Illuminate\Foundation\Auth\AuthenticatesUsers;
  
  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');
      }
  
      /**
       * Redirect the user to the GitHub authentication page.
       *
       * @return \Illuminate\Http\Response
       */
        public function redirectToProvider($social)
        {
            return Socialite::driver($social)-&gt;redirect();
        }
  
        /**
         * Obtain the user information from GitHub.
         *
         * @return \Illuminate\Http\Response
         */
        public function handleProviderCallback($social)
        {
            $user = Socialite::driver($social)-&gt;user();
  
            // $user-&gt;token;
        }
        /**
         * Get the needed authorization credentials from the request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return array
         */
          protected function credentials(Request $request)
          {
              $field = filter_var($request-&gt;get($this-&gt;username()), FILTER_VALIDATE_EMAIL)
                  ? $this-&gt;username()
                  : 'username';
  
              return [
                  $field =&gt; $request-&gt;get($this-&gt;username()),
                  'password' =&gt; $request-&gt;password,
              ];
          }
  }
  </code></pre>
  
  <p>App\Http\Controllers\Auth\RegisterController</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers\Auth;
  
  use App\User;
  use App\Http\Controllers\Controller;
  use Illuminate\Support\Facades\Validator;
  use Illuminate\Foundation\Auth\RegistersUsers;
  
  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 registration.
       *
       * @var string
       */
      protected $redirectTo = '/home';
  
      /**
       * 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
       */
      protected function validator(array $data)
      {
          return Validator::make($data, [
              'name' =&gt; 'required|string|max:255',
              'username' =&gt; 'required|string|max:20|unique:users',
              'phone' =&gt; 'required|string|max:20|unique:users',
              'email' =&gt; 'required|string|email|max:255|unique:users',
              'password' =&gt; 'required|string|min:6|confirmed',
              'gender' =&gt; 'required|bool',
          ]);
      }
  
      /**
       * Create a new user instance after a valid registration.
       *
       * @param  array  $data
       * @return \App\User
       */
      protected function create(array $data)
      {
          if($data['gender'])
            {
              $avatar = 'default/avatars/male.png';
            }
          else
            {
              $avatar = 'default/avatars/female.png';
            }
          return User::create([
              'name' =&gt; $data['name'],
              'gender' =&gt; $data['gender'],
              'username' =&gt; $data['username'],
              'phone' =&gt; $data['phone'],
              'email' =&gt; $data['email'],
              'password' =&gt; bcrypt($data['password']),
              'slug' =&gt; str_slug($data['username']),
              'avatar' =&gt; $avatar,
          ]);
      }
  }
  </code></pre>
  
  <p>Registration works fine and im able to store the user phone to the users table. I have changed the input type for email to text on views/auth/login.php and im able to login with either username or email.</p>
  
  <p>When i change the username to phone in LoginController im unable to login with phone as it says Method [phone] does not exist on [App\Http\Controllers\Auth\LoginController].</p>
  
  <p>When i add a method</p>
  
  <pre><code>/**
       * Create a new controller instance.
       *
       * @return void
       */
  public function phone()
      {
        return 'phone';
      }
  /**
         * Get the needed authorization credentials from the request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return array
         */
          protected function credentials(Request $request)
          {
              $field = filter_var($request-&gt;get($this-&gt;phone()), FILTER_VALIDATE_EMAIL)
                  ? $this-&gt;phone()
                  : 'phone';
  
              return [
                  $field =&gt; $request-&gt;get($this-&gt;phone()),
                  'password' =&gt; $request-&gt;password,
              ];
          }
  </code></pre>
  
  <p>It doesn't let me login with phone. Error These credentials do not match our records.
  Also Tried</p>
  
  <pre><code>/**
         * Get the needed authorization credentials from the request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return array
         */
          protected function credentials(Request $request)
          {
              if(is_numeric($request-&gt;get('email'))){
                return ['phone'=&gt;$request-&gt;get('email'),'password'=&gt;$request-&gt;get('password')];
              }
              return $request-&gt;only($this-&gt;username(), 'password'); 
          }
  </code></pre>
  
  <p>I'm able to login with phone or email now but not with username
  How to achieve login with username, email or phone</p>