Laravel Unique Email Validation on Update Example

Throught this Laravel unique email validation examle tutorial we will show you a easy short and best to add unique email validation in laravel on update the record. When we are creating or updating user information then we need to unique email validation.

The UNIQUE validation works on create time but when we update the user information then the validation occur the ‘this email is already taken’ message whereas we update the unique email info that time need to exits validation for that user.

So here we are going to show how can you add the laravel unique email address in create and update time. Unique Email Validation on Update example works in your laravel 5, laravel 6, laravel 7, laravel 8 or laravel 9 apps.

Example 1: in controller

Here you need to add the laravel email unique validation in your controller:

public function update(Request $request, User $user)
{
  $request->validate([
      'email' => 'required|email|unique:users,email,'.$user->id,
  ]);
}

Example 2: Form Request

Form requests are custom request classes that contain validation logic. If you are using the form request rules then you need to add something like below for more read here.

public function rules()
{
  return [
      'email' => 'required|email|unique:users,email,'.$this->user->id,
  ];
}

Example 3: Unique Ignore Method

If above example not working for resolving the unique email validation in laravel app then you need to add the Rule for ignoring the specific id just like below:

public function rules()
{
    return [
        'email' => ['required', 'email', \Illuminate\Validation\Rule::unique('users')->ignore($this->user()->id)]
    ];
}

Learn Also: Laravel Unique Validation where Clause Example Tutorial

I hope this Laravel Unique Email Validation on Update Example work for you.

2 thoughts on “Laravel Unique Email Validation on Update Example”

Leave a Comment