Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jkoopman's avatar

Updating existing product

Hi,

I'm working on a project where you can edit your product. Nothing special. I would accomplish the following:

  1. if user does not change the product url (slug) it should pass (check on id) - got that already working
  2. 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

0 likes
5 replies
UsmanBasharmal's avatar

You have set the slug unique that's why you are getting the error remove the unique it will work

jkoopman's avatar

Thank you for your reply.

Unfortunate it it's not working. When I remove the ->unique() from the slug in the migration and update the field to a same value as another product. It will store the same value.

In production this will means that 2 products have the same slug. Which determine the url of the product. For example: https://website.com/product/name-of-the-product/ where name-of-the-product is the slug.

UsmanBasharmal's avatar

of course, if you remove the unique it will insert duplicate values so the only way is to set the custom validation if the slug is matched.

ajithlal's avatar
ajithlal
Best Answer
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.