Exclude your own value from unique targets in Laravel
Problem
- -Created a form to edit and update user information and provided validation
- -Applied
unique
to username email address - -I want to change information other than the target of
unique
, but I get stuck inunique
and cannot proceed further.
Example
I want to change the DELETE_FLAG
for this job
I get an error about the job title
to which unique
is applied.
Code
The current code and validation are in this state
$ validator = Validator :: make ($ request-> all (), [
'edit_name' =>' required | max: 20 | unique: works, works_name',
'edit_flag' =>'boolean',
],,
[
'edit_name.required' =>'Please enter the job title. ',
'edit_name.max' =>'Please enter the job title within 20 characters. ',
'edit_name.unique' =>' This job title has already been registered. ',
'edit_flag.boolean' =>'Enter 0 or 1. ',
]);
solution
** Increase the argument of unique
**
$ validator = Validator :: make ($ request-> all (), [
'edit_name' =>' required | max: 20 | unique: works, works_name,'. $ Request-> edit_id.', Works_id',
'edit_flag' =>'boolean',
],,
[
'edit_name.required' =>'Please enter the job title. ',
'edit_name.max' =>'Please enter the job title within 20 characters. ',
'edit_name.unique' =>' This job title has already been registered. ',
'edit_flag.boolean' =>'Enter 0 or 1. ',
]);
The content of unique
is
** unique: unique The table name you want to check, the column name you want to unique check, the primary key of the record that has the data you want to exclude from the unique check.', The primary key of the record that has the data you want to exclude from the unique check. Key column name'
**
It has become.
The content of the process is
- -If the
unique check primary key
and theprimary key of the record with the data you want to exclude match
, the result ofunique
is not returned. - -If the primary key when the
unique check is done and the primary key of the record with the data you want to exclude do not match, the result of
unique` is returned.
(Should be)
Confirmation
You can now edit other data without changing the WORKS_NAME
that is the target of the unique
.
Data that has already been registered could be targeted for unique
.