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

david2000's avatar

validation and duplicate update() in laravel

In my form Voiture, I want to do a validation system for the fields immatriculation and num_vehicule

Here i an overview:

If I edit the first recording ie the value of the field num_vehicule 000001 per 0000032and that I validate, I have an error message because the value of the field immatriculation already exists.

I don't understand the problem... Do you have an idea please?

'immatriculation' => 'required|string|max:15|min:6|unique:voitures,immatriculation',
'num_vehicule' => 'required|string|max:6|min:6|unique:voitures,num_vehicule',
'fk_modele' => 'required'

Thank you

0 likes
5 replies
slev1n's avatar

unique:table,column - The field under validation must not exist within the given database table

exists:table,column - The field under validation must exist on a given database table.

So, unique for creating, exists for editing.

mabdullahsari's avatar
Level 16

The unique validator also accepts a third parameter, namely the id of the record to ignore the validation for.

So, if I may assume that you retrieved a vehicle model:

'immatriculation' => 'required|string|max:15|min:6|unique:voitures,immatriculation,' . $voiture->id ,
'num_vehicule' => 'required|string|max:6|min:6|unique:voitures,num_vehicule,' . $voiture->id,

Keep in mind that you should only do this for updates, not for creations.

1 like
david2000's avatar

@mabdullahsari : Thank you for your help, I have an error message Undefined variable: voiture ? Where I have to find this variable ?

siangboon's avatar

should understanding first before you copy and paste....

mabdullahsari meant the 3rd parameter is to pass the id of the model you trying to update, so he just assume $voiture is the model your trying to update... what your need to do is just replacing the $voiture with the correct model.

2 likes
siangboon's avatar

you are confusing us, you were asking about validation issue but now you are showing something not related as no validation in the update method.

anyhow, you could pass the model $voiture instead of $id in the update method and remember to update the web.php parameter as well if you do so.

1 like

Please or to participate in this conversation.