简体   繁体   中英

How to update without changing the value of unique fields?

I update user data. Form of update is fill current user data. And if I don't change email validation show me error that this email already been taken. I use Rule::unique that resolve this problem but this not working?

Fix previous answer

class CustomerRequest extends FormRequest
{
    public function rules()
    {
     return [
       'first_name'   => 'min:3|max:20|regex:/^[a-zA-z-0-9]+$/u',
       'last_name'    => 'min:3|max:30|regex:/^[a-zA-z-0-9]+$/u',
       ['email', Rule::unique('customers')->ignore($this->customer->id)]      
     ];
}

Also in your controller must receive

public function update(CustomerRequest $request, Customer $customer)
    {
        //Code
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM