login with username or email in laravel 5
In this post we will give you information about login with username or email in laravel 5. Hear we will give you detail about login with username or email in laravel 5And how to use it also give you demo for it if it is necessary.
Sometime we will have to give chance to user to login with their username or email within the application.
In login form there will be one field and user will have option to enter either email or username with their password to login.
- public functionlogin(Request $request)
- {
- $field=filter_var($request->input('email_or_username'), FILTER_VALIDATE_EMAIL)?'email':'username';
- $request->merge([$field=>$request->input('email_or_username')]);
- if(Auth::attempt($request->only($field,'password')))
- {
- returnredirect('/');
- }
- returnredirect('/login')->withErrors([
- 'error'=>'These credentials do not match our records.',
- ]);
- }
public function login(Request $request) { $field = filter_var($request->input('email_or_username'), FILTER_VALIDATE_EMAIL) ? 'email' : 'username'; $request->merge([$field => $request->input('email_or_username')]); if (Auth::attempt($request->only($field, 'password'))) { return redirect('/'); } return redirect('/login')->withErrors([ 'error' => 'These credentials do not match our records.', ]); }
In the above example, we use PHP filter FILTER_VALIDATE_EMAIL
to validate input data is a valid email address or not.
If request have valid email address then we will authenticate with “email” field otherwise we will authenticate with “username” field.
Try this code when you need to login with username or email field.
Hope this code and post will helped you for implement login with username or email in laravel 5. 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