Ad

Laravel 5.6. The Unique Validation Rule. Exclude From The Scan The Field Of The Current Record, If It Did Not Change

- 1 answer

I have a validation class:

    class AnalyseRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        'code' => 'required|code_format|unique:analyses',
    }
}

The code field stores the code, which must be unique. There is a template where this code is displayed for editing. I edit other fields, I do not touch the code. I keep my form. Then this rule is triggered. Tell me please, how can I set up the validator so that it works correctly when this value is edited to a new one?

Ad

Answer

Use:

'code' => 'required|code_format|unique:analyses,code,'.$this->id,
Ad
source: stackoverflow.com
Ad