Login with user name instead of email in Laravel

Default laravel login is set to work with email and password but there might be some instance where login with an email address is not desirable and you might want to change this to login with a username.

The advantage of logging in with an email address is that you can be sure it will be unique. The drawback is that it is a bit long to type and some people are not comfortable giving their email address, so depending on your type of application, where having the email address is not essential, it makes sense to offer to login with a username instead.

In Laravel 5.8

Add the following function in app/Http/Controllers/Auth/LoginController.php

/**
*Use username instead of default (email address)
*for authentication
**/
public function username(){
    return ('name');
}

in the return(‘name’) statement, ‘name’ stand for the column name in your user table in your database you want to use. In my case the column is ‘name’.

in resources/views/auth/login.blade.php change the email login field with a username field as follow:

<div class="form-group row">
    <label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
    <div class="col-md-6">
       <input id="name" type="text" class="form-control" name="name" required>
  </div>
</div>

As you are now using the username instead of the email for logging in, you need to make sure the username is the unique. To do that you will need to modify your migrations file for create_users_table.php

table->string('name')->unique(); 

And you are all set.