Laravel:如何更改默认的“身份验证密码"字段名称?
我目前正在做我的第一个laravel项目,但遇到了问题.
I am currently working on my first laravel project and I am facing a problem.
如果您有使用laravel的经验,您可能知道通过调用 php artisan make:auth
,您将获得处理登录和注册的预定义机制.
If you have experience with laravel you probably know that by calling php artisan make:auth
you will get a predefined mechanism that handles login and registration.
此机制设置为可以理解几个常用词,以使整个过程自动化.
This mechanism is set to understand a couple of commonly used words in order to automate the whole procedure.
在我的情况下出现的问题是我使用的是oracle db,它不会让我有一个名为 password
的表列,因为它是系统关键字,并且在出现以下情况时会引发错误尝试插入用户.
The problem that occurs in my case is that I am using oracle db and it won't let me have a table column with the name of password
because its a system keyword and it throws errors when trying to insert a user.
到目前为止,我已经尝试将我的 password
列更改为 passwd
,并且它可以按预期在我的注册表格中使用.用户行已成功插入,我的页面已重定向到/home.
So far, I have tried to change my password
column to passwd
and it worked in my registration form as expected. The User row was successfully inserted and my page was redirected to /home.
但是当我尝试注销然后重新登录时,出现此错误,告诉我我的凭据不正确:
But when I try to logout and then relogin, I get this error telling me that my credentials are not correct:
对于我的代码,我已经更改了我的 RegisterController.php
,使其使用用户名而不是电子邮件
As for my code, I have changed my RegisterController.php
so that it takes username instead of email
protected function validator(array $data)
{
return Validator::make($data, [
'username' => 'required|max:50|unique:ECON_USERS',
'passwd' => 'required|min:6|confirmed',
]);
}
protected function create(array $data)
{
return User::create([
'username' => $data['username'],
'passwd' => bcrypt($data['passwd'])
]);
}
用户$ fillable
The User $fillable
protected $fillable = [
'username', 'passwd'
];
我猜测Auth尝试使用电子邮件
而不是 username
进行身份验证,或者Auth搜索的是 password
而不是密码
.
I am guessing that Auth is trying to authenticate with email
and not username
or that Auth is searching for password
and not passwd
.
For having username
instead of email
, you can overwrite username() in your LoginController.php
/**
* Get the login username to be used by the controller.
*
* @return string
*/
public function username()
{
return 'username';
}
And for passwd
instead of password
, you can do define an accessor in your App\User.php
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->passwd;
}
login.blade.php :用 username
替换 email
输入,但请勿更改名称输入密码
.
login.blade.php : Replace email
input with username
but do not change the name of the input for password
.