Laravel Login with Username or Email Example
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.
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')))
'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...
We are Recommending you
- Laravel Model Disable Primary Key & Auto Increment Example
- Laravel Carbon Time Format AM PM Example Code
- Laravel Sum Query with Where Condition Example
- Laravel Seeder from CSV File Example
- Laravel Eloquent Select Single Column to Array Example
- How to Image Upload in Laravel Vapor?
- Laravel One to Many Eloquent Relationship Tutorial
- Laravel Many to Many Eloquent Relationship Tutorial
- How to Return JSON Response in Laravel?
- Laravel 5 manual pagination with array example
- How to Resolve Laravel Blank Page on Server?
- Laravel XSS Protection Middleware Example
- Laravel Join with Subquery in Query Builder Example
- How to Create Custom Helper Function in Laravel?
- How to Send Mail using Gmail SMTP in Laravel?