Laravel: How can i change the default Auth Password field name

前端 未结 7 1670
遇见更好的自我
遇见更好的自我 2020-11-27 06:08

I\'m currently working on my first laravel project and i\'m facing a problem.

If you have experience with laravel you probably know that by calling php artisan

相关标签:
7条回答
  • 2020-11-27 06:37

    In Laravel 5.7 beside above answer you must change EloquentUserProvider class. search in the file for 'password' word in lines (107,117, and 140) you found 'password' word and change it with new name, and this is all solustion.

    • In User Model add this method :
    public function getAuthPassword(){
        return $this->new_password_name;
    }
    
    • In LoginController add this :
    protected function validateLogin(Request $request){
        $this->validate($request, [
            $this->username() => 'required',
            'new_password_name' => 'required',
        ]);
    }
    protected function credentials(Request $request)
    {
        return $request->only($this->username(), 'new_password_name');
    }
    public function username()
    {
        return 'new_username';//or new email name if you changed
    }
    
    • In login.blade.php change id and name of html element.
    • In EloquentUserProvider class inside validateCredentials and retrieveByCredentials function change 'password' word with the new name.

    Edit :I change EloquentUserProvider class but if you think changing laravel class is a bad practice you can create custom provider and override the retrieveByCredentials and validateCredentials functions in the custom provider.

    0 讨论(0)
提交回复
热议问题