Laravel 5 – login with username or email example
In this post we will give you information about Laravel 5 – login with username or email example. Hear we will give you detail about Laravel 5 – login with username or email exampleAnd how to use it also give you demo for it if it is necessary.
Today, we will learn how to allow login with username or email in laravel 5 application. If you see on some website, they allow email or username both for login or email, username, mobile no, id etc. i mean they provide several way for login.
In this example, you can learn how to make it possible to username or email for login in php laravel 5. we have to simply use PHP filter
“FILTER_VALIDATE_EMAIL” to validate is it username or email So, basically you have to write your login method like as bellow i gave you example:
Let’s see bellow example and put it on your login method.
Login:
public function login(Request $request)
{
$field = filter_var($request->usernameOrEmail, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
$request->merge([$field => $request->usernameOrEmail]);
if (auth()->attempt($request->only($field, 'password')))
{
return redirect('/');
}
return redirect('login')->withErrors([
'error' => 'These credentials do not match our records.',
]);
}
As you can see above code, it is very simple and we can use it simply. So, use it.
I hope it can help you…
Hope this code and post will helped you for implement Laravel 5 – login with username or email example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs