What you have to do is modify the migration, the Model, the controller and the View of the Login form.
Just yesterday I did a gist on github to explain this. How to login with username instead of by email in Laravel . I also give you the explanation and the code here. I hope it is what you need.
Greetings
The controller App\Http\Controllers\Auth\LoginController
implements the trait AuthenticatesUsers
and this is responsible for performing the validation of the Login. The validateLogin()
method validates the field email
by default but it obtains it from the username()
method that returns the email
field, so if we want to validate the login for another field (In this example it will be the field < strong> 'username' ) we must:
1- In the migration of the table users
( 2014_10_12_000000_create_users_table.php
) add the field username
it is important that this is of type unique
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username')->unique();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
2- In the model App\User
add the field username
to the fillables.
protected $fillable = [
'username', 'name', 'email', 'password',
];
3- In the controller App\Http\Controllers\Auth\LoginController
add the method username()
public function username()
{
return 'username';
}
4- In the blade view auth/login.blade.php
modify the login form. Instead of a field of type email
we place as field text
and we also have to change the name.
<div class="form-group row">
<label for="username" class="col-sm-4 col-form-label text-md-right">{{ __('Username') }}</label>
<div class="col-md-6">
<input id="email" type="text" class="form-control{{ $errors->has('username') ? ' is-invalid' : '' }}" name="username" value="{{ old('username') }}" required autofocus>
@if ($errors->has('username'))
<span class="invalid-feedback">
<strong>{{ $errors->first('username') }}</strong>
</span>
@endif
</div>
</div>
And voila, we can use the default auth of Laravel but now we log in using username instead of email