0

I am coded custom user system in Laravel 6.X. In my Login Controller i have this validation code:

    $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
            }
        },
    ],
    ]);

I am store my passwords with SHA256 Hashing. If password not equals to database password then return an error message to user. But i am getting an exception "Undefined variable: email".

1 Answers1

2

Check out this answer: https://stackoverflow.com/a/11086796/7897328. I think you need to use the use keyword.

Your code:

function ($attribute, $value, $fail) {

Your code with the use keyword:

function ($attribute, $value, $fail) use ($email) {
SafwanA
  • 85
  • 1
  • 6