0
votes

Laravel Framework 5.6.38

I am trying to change the default username auth check from 'email' to 'id', So i put the username() method inside User model file to override the default username method

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password','id'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function username()
    {
        return 'id';
    }

}

It's not working with me . unless i change it from laravel vendor file Illuminate\Foundation\Auth\AuthenticatesUsers.php

 /**
     * Get the login username to be used by the controller.
     *
     * @return string
     */
    public function username()
    {
        return 'kocid';
    }

The login now working but when i run composer update , the file return to it default, so i need a solution to successfully override this method using User model.

2

2 Answers

2
votes

According to the documentation, https://laravel.com/docs/5.6/authentication#authentication-quickstart...you should do that in your LoginController

public function username()
{
    return 'id';
}
0
votes

Add the username method to your LoginController to override the method from the AuthenticatesUsers trait (Docs - Username Customization).

public function username()
{
    return 'id';
}