Laravel:如何更改默认的“身份验证密码"字段名称 [英] Laravel: How can i change the default Auth Password field name

查看:213
本文介绍了Laravel:如何更改默认的“身份验证密码"字段名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在做我的第一个laravel项目,但遇到了问题.

I'm currently working on my first laravel project and i'm 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的表列,因为它是系统关键字,并且在尝试插入a时会引发错误.用户.

The problem that occurs in my case is that i'm 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've 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've 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尝试使用email而不是username进行身份验证,或者Auth搜索的是password而不是passwd

I'm guessing that Auth is trying to authenticate with email and not username or that Auth is searching for password and not passwd

推荐答案

要使用username而不是email,您可以覆盖

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';
}

对于passwd而不是password,您可以定义

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输入,但不要更改password的输入名称.

login.blade.php : Replace email input with username but do not change the name of the input for password.

这篇关于Laravel:如何更改默认的“身份验证密码"字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆