Skip to main content

Unique email validation in Laravel when re-saving model

When you validate an email in Laravel, you have the ability to specify whether you want a specific model to be excluded. The reason why you would exclude a model is for example, if you have a user account and the user needs to update their first name, but their email stays the same. Typically, Laravel would re-save the whole form and also validate the email, but because that user is already using the email, it will spit back a “unique” validation error mentioning the email already exists in the system (even though it is still being used by said user.

To get around the issue, you can do this, for instance you are updating a user model,

public function update(User $user, Request $request)
           'email' => 'required|unique:users,email,' . $user->id,

As you can see, the $user->id is what is being excluded from the validation.

Leave a Reply