我在Laravel 6.X中编码自定义用户系统。在我的登录控制器中,我有以下验证代码:
$email = $request->email;
$validator = Validator::make($request->all(), [
'email' => 'bail|email|required|max:32|exists:users,email',
'password' => ['required','min:1','max:32',
function ($attribute, $value, $fail) {
$users = DB::table('users')->where('email', $email)->get(); // Get user info (Exception is in here)
if (hash('sha256', $value) !== $users[0]->Password) { // Check if hashed password equals
$fail($attribute.' is wrong.'); // If not equal then return error message
}
},
],
]);
我使用SHA256哈希存储密码。如果密码不等于数据库密码,则向用户返回错误消息。但是我收到一个异常“未定义变量:电子邮件”。
答案 0 :(得分:2)
查看以下答案:https://stackoverflow.com/a/11086796/7897328。我认为您需要使用use
关键字。
您的代码:
function ($attribute, $value, $fail) {
您的带有use
关键字的代码:
function ($attribute, $value, $fail) use ($email) {