Search code examples
laravelauthenticationpasswordsforgot-password

Error Laravel Password Reset: Column 'email' Cannot Be Null


I'm working on a Laravel application where I have implemented the password reset feature. However, I'm consistently running into the following error:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'email' cannot be null (Connection: main, SQL: insert into password_resets (email, token, created_at) values (?, $2y$10$sFSxC9pgIbmRisHh4RZK5.j0cZNLlmogdlbiokfnKDLoSv4ng.SHW, 2024-01-29 14:44:11))",

Here's my code snippet from the 'sendResetPasswordEmail' function:

public function sendResetPasswordEmail(Request $request)
{
    $request->validate(['email' => 'required|email']);

    $email = $request->input('email');

    $status = Password::sendResetLink(['email' => $email]);

    return $status === Password::RESET_LINK_SENT
        ? back()->with(['status' => __($status)])
        : back()->withErrors(['email' => __($status)]);
}

This is the body of my post request:

{
    "email": "[email protected]"
}

My Pathetic Attempt at Debugging:

  • I have confirmed that the 'email' is present in the request body and extracted correctly.
  • I've verified my Laravel version, ensuring that it is up-to-date and follows the current documentation.
  • I've made the 'email' field nullable in the password_resets table. However, it registers as an null value.

Solution

  • The problem was that in my Users table in the database, the email field had a capital 'E' like 'Email.' However, Laravel by default searches for 'email' with a lowercase 'e,' and that's why.

    If you don't want to modify the column's name on your database you can paste the following code on your User's model:

    public function getEmailForPasswordReset(): string
    {
            return $this->Email;
    }
    

    This code will overwrite the getEmailForPasswordReset on your User's model and you can customize the field that's going to be researched. Hope it helps some lost soul like mine!