1

I have a user admin panel where admin can create update users and while users have a unique email field. While updating when I don't wanna change the email address it should be as it is. but throwing me error like: The email has already been taken.

I already tried:

  1. How to prevent insert email if already exist with specific id in laravel?

  2. https://laravel.com/docs/5.2/validation#rule-unique

and my user model rule

'email' => 'required|unique:users,email,{$user->id}'

what I'm missing here? please help

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
Tridev Shrestha
  • 447
  • 7
  • 21

1 Answers1

1

If the user only changes the name field and not the e-mail field, you do not want a validation error to be thrown because the user is already the owner of the e-mail address.

you can use like:

'email' => [
        'required',
        Rule::unique('users')->ignore($user->id),
],

Read Under the Section Forcing A Unique Rule To Ignore A Given ID: LINK

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64