我在 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 哈希存儲我的密碼。如果密碼不等于數據庫密碼,則向用戶返回錯誤消息。但我得到一個例外“未定義的變量:電子郵件”。
1 回答

慕無忌1623718
TA貢獻1744條經驗 獲得超4個贊
看看這個答案:https ://stackoverflow.com/a/11086796/7897328 。我認為您需要使用use
關鍵字。
你的代碼:
function ($attribute, $value, $fail) {
帶有use
關鍵字的代碼:
function ($attribute, $value, $fail) use ($email) {
- 1 回答
- 0 關注
- 137 瀏覽
添加回答
舉報
0/150
提交
取消