You have set the slug unique that's why you are getting the error remove the unique it will work
May 21, 2020
5
Level 1
Updating existing product
Hi,
I'm working on a project where you can edit your product. Nothing special. I would accomplish the following:
- if user does not change the product url (slug) it should pass (check on id) - got that already working
- if user changes the value of the product url (slug) it should check in the table if that value is unique
I use this request validation for that. Which works for option 1.
$this->validate($request, [
'name' => 'required',
'description' => 'required',
'slug' => 'required|unique:products,id,'.$id.'',
]);
But when I update the slug too a new value which is the same as an other product in the table. I receive the following error.
Integrity constraint violation: 1062 Duplicate entry 'seo-url2' for key 'products_slug_unique
Does anyone have a solution for this? Thank you in advance
Level 18
try updating your rule
'slug' => 'required|unique:products,slug,'.$id,
make sure that the value of the $id is not null
Please or to participate in this conversation.