Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kea_rajab's avatar

Laravel 8.x login with phone number and not email

I have been trying to solve this enigma by trying to override the username function but what it does is just reloading and brings me back to the login and saying "These credentials do not match our records", sign up works just fine but on this nothing progressive happen, also i have tried overriding the credentials, also not bearing any fruits, here is my logincontroller


namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Providers\RouteServiceProvider;
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 = RouteServiceProvider::HOME;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
        $this->username = $this->findUsername();
    }

    public function findUsername()
    {
        $login = request()->input('email');

        $fieldType = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'phone_number';

        request()->merge([$fieldType => $login]);

        return $fieldType;
    }

}

Please help, i have searched but no where to find the proper answer for this solution.

0 likes
8 replies
Vixo's avatar
Vixo
Best Answer
Level 25

By default, Laravel uses the email field for authentication. If you would like to customize this, you may define a username method on your LoginController :

public function username()
{
    return 'phone';
}
kea_rajab's avatar

@vixo in my users table am using phone_number as the columns name, i tried this

return 'phone_number';
}

But it just reloads the login page

kea_rajab's avatar

just solved this problem, hope it will help someone,

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Providers\RouteServiceProvider;
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 = RouteServiceProvider::HOME;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function username(){
        return 'phone_number';
    }

}

what i missed is changing the view's email input to phone_number

<input id="phone_number" type="text" placeholder="Phone Number:" class="form-control @error('phone_number') is-invalid @enderror" name="phone_number" required  autofocus>

@error('phone_number')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror

thanks @vixo

MichalOravec's avatar

@kea_rajab Why did you mark your answer as the best answer, when @vixo helped you. And that you have to change an input name it's pretty obvious.

2 likes
kea_rajab's avatar

well, i was curious if you can set your own reply as best answer, and among other weird things, before i started paying for laracast, i could mark episodes complete even if i could not see them, and so i wanted to see if you could have two best answers in one question, or can you give yourself a badge of best reply by giving yourself a best reply, it was just my curiosity! and of course if you happen to contact jeffery right away, you can tell him to fix that marking capabilities to users who are not paying... ;)

Brandon Eichhorn's avatar

Make a topic and be a bit more descriptive. Laracasts serves as an assistance for developers, we are not magicians, even though Laravel is magically beautiful. :)

Please or to participate in this conversation.