Laravel: Validation unique when update and create
11,177
In Unique Rule
unique:table,column,except,idColumn
3rd param is for value for column to except
and 4th is for column to except
I am also using one validation method for both insert
and update
like this way
public function validateItems($requestAll){
$id = isset($requestAll['id']) ? ','.$requestAll['id'].',id':'';
$rules = [
'cod_upb' => 'required|max:10|unique:equipos,cod_upb,'.$id,
];
return Validator::make($requestAll, $rules);
}
Just change code for $requestAll['id']
as I took for id
, in your case it might be $this->id
Related videos on Youtube
Author by
Maria Camila Cuadrado
Rookie programmer!!! Don't get mad if I ask really simple questions.
Updated on June 04, 2022Comments
-
Maria Camila Cuadrado almost 2 years
I have a Request with rules where I validate that one field is unique.
When I use:
'cod_upb' => 'required|max:10|unique:equipos,cod_upb,'.$this->id,
It works in the create method because it doesn't allow me to repeat the field in the database. But when I try to update it says that the field already exist or is taken.
Then I tried with:
'cod_upb' => 'required|max:10|unique:equipos,id,'.$this->id,
Which works on the update, but it let me create new items repeating the field.
How can I fix that?
-
Maria Camila Cuadrado about 6 yearsMy primary key is a field named "id" and it is an auto increment field in mysql
-
tprj29 about 6 yearsWhat does $this->id represent?
-
-
Niklesh Raut about 6 yearsPlease also add
use Illuminate\Validation\Rule;
to useRule::unique
and add comment to make value of$this->id
of 4 param in update and 2 param in insert -
Maria Camila Cuadrado about 6 yearsWhen I do what you mention it works on the create function but it doesn't work on the update. In the update it keeps saying that cod_upb already exists, which i assume is not ignoring the id
-
tprj29 about 6 years@MariaCamilaCuadrado What exactly is in $this->id ?
-
tprj29 about 6 years@C2486 if you ever developed with Laravel before or you are developing with Laravel at the moment this is common knowledge. And i have no idea what you are trying to say about the
$this->id
comment -
Niklesh Raut about 6 years@tprj29: OP is already using like as you suggested
'cod_upb' => 'required|max:10|unique:equipos,cod_upb,'.$this->id,
she is saying it is working forinsert
but notupdate
, but yes the second suggestionRule
would work. for thecommon knowledge
it is many things are common but people are still gets stuck. -
Niklesh Raut about 6 yearsTo ignore
id
while updating it should be like$id = isset($requestAll['id']) ? ','.$requestAll['id'].',id':'';
as I suggested. -
tprj29 about 6 years@c2486 I think op is using the wrong id. On insert this isn't a problem because there is no other record with the exact
cod_upb
but on update the id that is ignored is incorrect and this will cause a problem because there is already a record with thecod_upb
. This would make sense but OP should clarify what$this->id
represents. The whole ignoring id on insert etc. is not the question but you make a valid point if you want to reuse the validation logic you should find out if you should add id yes or no.