如何更新Laravel集体中的独特字段? [重复]
问题描述:
This question already has an answer here:
- Laravel: Validation unique on update 16 answers
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:
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