Exclude your own value from unique targets in Laravel

Sep 7, 2020 PHP Laravel

Problem

Example

I want to change the DELETE_FLAG for this job image.png

I get an error about the job title to which unique is applied. image.png

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

(Should be)

Confirmation

You can now edit other data without changing the WORKS_NAME that is the target of the unique. image.png

Data that has already been registered could be targeted for unique. image.png

References