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

M@rty's avatar
Level 1

Validation unique Rule in form request class -> ignore current Id

Hello, I'm asking for a piece of advice and help on how can I add unique rules on name and code when a user tries to store and update product details

Here is the code from the ProductStoreRequest Form request class code

return [
            'name'   => ['required', Rule::unique('products')->ignore($this->id)],
            'code'   => ['required', Rule::unique('products')->ignore($this->id)],
            'qty' => ['numeric'],
            'product_tag' => ['required']
        ]; 

when the user tries to store it works perfectly.

if the user tries to update the same product then it should ignore the current id but it gives a validation message for unique value

Here is the API response on update

"errors": [
        {
            "name": "The name has already been taken."
        },
        {
            "code": "The code has already been taken."
        }
    ]

Is something am I missing, please help me with this! Many thanks!

0 likes
26 replies
Sinnbeck's avatar

Try running dd on the id to check if its correct

dd($this->id);
M@rty's avatar
Level 1

Hey, I'm getting null while dd();, but don't know why!

MichalOravec's avatar

If you are using route model binding then

->ignore($this->route('product')->id)
1 like
M@rty's avatar
Level 1

Hey, I tried your solution but it gives me an error

ErrorException: Trying to get property 'id' of non-object in file E:\www\nginx\...\app\Http\Requests\ProductStoreRequest.php on line 32

M@rty's avatar
Level 1

Hi, sorry didn't get you!

but when I try to

'name' => ['required', Rule::unique('products')->ignore($this->product->id)],

it works on the update product function

but the same error will be expected when the products table may empty

 ErrorException: Trying to get property 'id' of non-object in file E:\www\nginx\...\app\Http\Requests\ProductStoreRequest.php on line 32
Sinnbeck's avatar

You can add a fallback

'name' => ['required', Rule::unique('products')->ignore($this->product->id ?? 0)],
MichalOravec's avatar

@sinnbeck $this->product->id is wrong.

Let's imagine you have

<input name="product">

then you have a problem.

Sinnbeck's avatar

@michaloravec +1 Good point :)

But using a fallback (null coalescing operator) is still valid :)

->ignore($this->route('product')->id ?? 0)
M@rty's avatar
Level 1

One more thing I need your guidance

I don't have to manage two separate form request for store product and update product

it is not necessary, right?

MichalOravec's avatar

I always use two separated form request class with inheritance.

Sinnbeck's avatar

But you can use one if you want. It's a matter of personal preference

M@rty's avatar
Level 1

Thank you @michaloravec @sinnbeck this answer helps!

Could you please elabroate the $this->route('product')->id statement?

seems I have confusion for this :)

Christofer's avatar

Can someone please explain why "unique" validation checks the existing record. What are the issues that can occur when setting this to automatic?

Or, at least why can't we have a global config that has it automatically check the record for an ID and if it has an ID, to just ignore self. Would be nice to be able to toggle that option on or off.

PS: only thing I can think of is when I have a [Create] button automatically create a blank record, save it, and redirect to an Edit page. In this case, the default "Unique ignore self" would allow multiple records to have the default value. Though I still can't see how this is worse than the current way unique works.

mrbegginerak's avatar

this code helped me.

return [
         'name'   => ['required', Rule::unique('products','name')->ignore($this->id,'id')],
         'code'   => ['required', Rule::unique('products','code')->ignore($this->id,'id')],
         'qty' => ['numeric'],
         'product_tag' => ['required']
        ]; 

return [
     'name'   => ['required', Rule::unique(Product::class,'name')->ignore($this->id,'id')],
     'code'   => ['required', Rule::unique(Product::class,'code')->ignore($this->id,'id')],
     'qty' => ['numeric'],
    'product_tag' => ['required']
        ]; 
Snapey's avatar

@mrbegginerak if you have a question then create a question

Dont just post random code and expect help

mrbegginerak's avatar

@Snapey I did not post code; I posted an solution. I had also faced this issue and checked this post, but it did not help as much as I expected. So when I found the solution, I posted the answer.

mrbegginerak's avatar

@Snapey Thank you for your kind words. You're absolutely right; there are often multiple ways to solve a problem. I found this particular solution to be the best fit for my needs, which is why I decided to share it. If anyone else encounters a similar issue, there's a chance that it could be helpful to them as well.

Basem's avatar
public function rules(): array
    {
        return [
            'email' => 'required|string|email|max:255|unique:users,email,'.request()->user()->id
        ];
    }
1 like

Please or to participate in this conversation.