laravel login with username or email code example
Example 1: login with email and phone laravel
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'), FILTER_VALIDATE_EMAIL)) {
return ['email' => $request->get('email'), 'password'=>$request->get('password')];
}
return ['username' => $request->get('email'), 'password'=>$request->get('password')];
}
Example 2: laravel using username instead of email
All we need is add a method in app/Http/Controllers/Auth/LoginController.php:
public function username()
{
return 'username';
}
Example 3: username or email validation in laravel
$input = $request->only('account');
$rules = [
'account' => 'required'
];
if (filter_var($input['account'], FILTER_VALIDATE_EMAIL)) {
$rules['account'] .= '|exists:users,email';
} else {
$rules['account'] .= '|exists:users,username';
}
$messages = [
'account.required' => 'Enter username or email to login.',
];
$validator = Validator::make($input, $rules, $messages);