Login With Email Or Username In One Field in Laravel
I will tell you the main part where you should change this.
create_user_table
// add this extra field
$table->string('username');
In the register blade, you should add a username field also so the user can choose the email or username that he should use for login. I skip that and go to the login method.
login.blade.php
<!-- replace this email field with login -->
<input id="login" name="login" type="text" class="form-control">
In LoginContoller here is an update code below. You find this controller in app/Http/Controllers/Auth
LoginController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Login username to be used by the controller.
*
* @var string
*/
protected $username;
/**
* Create a new controller instance.
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
$this->username = $this->findUsername();
}
/**
* Get the login username to be used by the controller.
*
* @return string
*/
public function findUsername()
{
$login = request()->input('login');
$fieldType = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
request()->merge([$fieldType => $login]);
return $fieldType;
}
/**
* Get username property.
*
* @return string
*/
public function username()
{
return $this->username;
}
}
If you need more about it here is the link