如何更新Laravel集体中的独特字段? [重复]

如何更新Laravel集体中的独特字段?  [重复]

问题描述:

This question already has an answer here:

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

</div>

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