所以我已经发现了一些问题,这些问题表明您必须覆盖 getAuthPassword() 才能从数据库中提供密码列的自定义名称。尝试将此方法与数据库中的列同名,但没有用。它仍然会抛出这个错误: undefined index :密码。
这是授权:
if (Auth::attempt(Input::only('user_displayName'), Input::get('user_password')))
尝试在表单和 Controller 中将 user_password 更改为 password 无效。
所以问题是,如果我在数据库中有一个名为“user_password”的列,是否有办法使 Auth 工作?
P.S 检查了我发现的每一个旧的解决方案
编辑
用户表结构:
+======================+
| User |
+======================+
| user_id |
+----------------------+
| user_displayName |
+----------------------+
| user_fname |
+----------------------+
| user_lname |
+----------------------+
| user_email |
+----------------------+
| user_password |
+----------------------+
| created_at |
+----------------------+
| updated_at |
+----------------------+
最佳答案
tldr;只要您的用户模型正确实现接口(interface),您就可以随意命名密码字段。
但是您不能将不同的数组键传递给 Auth::attempt
方法 - 只有 password
索引可以存在
首先你做错了——你需要传递一组凭证作为第一个参数:
if (Auth::attempt(Input::only('user_displayName', 'user_password')))
接下来,不幸的是 Eloquent
提供程序在代码中硬编码了 password
数组索引,所以你不能将 user_password
传递给 attempt
方法。
这就是你需要的:
$credentials = Input::only('user_displayName');
$credentials['password'] = Input::get('user_password');
if (Auth::attempt($credentials))
// or simply rename the input in your form to password and:
if (Auth::attempt(Input::only('user_displayName', 'password')))
关于php - laravel 4 自定义命名密码列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26002552/