Skip to content
Advertisement

Unknown Column Error when using custom column name for email – Laravel Fortify

I have a database with a column named mail for the users table, but in Laravel it’s email. My problem is now the forgot-password function.

I will always get the following error:

SQLSTATE[23000]: Integrity constraint violation: 
1048 Column 'email' cannot be null 
(SQL: insert into `password_resets` (`email`, `token`, `created_at`) values 
(?, $2y$10$....., 2022-01-03 16:55:22))

I created a custom route for forgot-password, but that’s not working somehow.

Route::post('/forgot-password', function (Request $request) {
    
    $request->validate([
        'mail' => ['required', 'email'],
        'username' => ['required'],
    ]);
    $status = Password::sendResetLink(
        $request->only('username', 'mail')
    );
    
    return $status === Password::RESET_LINK_SENT
        ? back()->with(['status' => __($status)])
        : back()->withErrors(['email' => __($status)]);
})->middleware('guest')->name('password.mail');````

In the fortify.php I set 'email' => 'mail', but there is no difference. Maybe I did something extremely dumb, but I’m new to Laravel.

So how can I fix this?

It’s not possible to rename the mail column to email and it would be dumb to create a second column in the users table that’s just named email with the same content.

Regards.

Advertisement

Answer

You should override the getEmailForPasswordReset method on the model you are trying to use. The Token Repository will ask the model for the value of what ever the email field is by calling getEmailForPasswordReset on the model.

public function getEmailForPasswordReset()
{
    return $this->mail;
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement