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

srikanthgopi's avatar

Login with Username, email or phone

I want to a user to be able to login with either username, email or phone from a single field

In my LoginController i tried

protected function credentials(Request $request)
        {
            if(is_numeric($request->get('email'))){
              return ['phone'=>$request->get('email'),'password'=>$request->get('password')];
            }
            elseif (filter_var($request->get('email'))) {
              return ['email' => $request->get('email'), 'password'=>$request->get('password')];
            }
              return $request->only($this->username(), 'password');
        }

I'm able to login with phone and email but not username. when i change it to

protected function credentials(Request $request)
        {
            if(is_numeric($request->get('email'))){
              return ['phone'=>$request->get('email'),'password'=>$request->get('password')];
            }
            elseif (filter_var($request->get('email'))) {
              return ['username' => $request->get('email'), 'password'=>$request->get('password')];
            }
              return $request->only($this->email(), 'password');
        }

I'm able to login with phone or username but not with email. How to achieve login any of the three phone, email or username. I'm using laravel 5.5 and i have not mentioned any rules.

0 likes
6 replies
PrinceMinky's avatar
Level 11

Would this work?

If is numeric, login with number. If is email, login with email address. Else try username?

if(is_numeric($request->get('email'))){
    return ['phone'=>$request->get('email'),'password'=>$request->get('password')];
}
elseif (filter_var($request->get('email'), FILTER_VALIDATE_EMAIL)) {
    return ['email' => $request->get('email'), 'password'=>$request->get('password')];
}
return ['username' => $request->get('email'), 'password'=>$request->get('password')];

3 likes
srikanthgopi's avatar

It says Parse error: syntax error, unexpected 'return' (T_RETURN), expecting ';'

srikanthgopi's avatar

@miiikkeyyyy Works like a charm. Thank you so much. I've been showing my code on multiple platforms like stackoverflow and other IRC chats, all they were suggesting me to use a different thinigs like adding rules or trash. But i was just looking for the mistake in my code which no one corrected.

Thank you so much :) @miiikkeyyyy

PrinceMinky's avatar

Anytime Bro. Glad I could help.

Message me anytime for help. I work with larval as a hobby. So I'm always available to help

Please or to participate in this conversation.