Good afternoon, comrades. Tell me who knows how to add more than 1 unique field to the validator class in laravel. If we assume I have unique fields used in conjunction with (for example, series and card number).

return Validator::make($data, [ 'serial' => 'required|min:3|unique:cards,serial', 'number' => 'required|min:8|max:8|regex:/[0-9]{8,8}/', 'date_close' => 'required', ]); 

I tried it but it does not help, it still knocks out an error at the first entry of the serial, and does not even get to check the number. And in theory, should swear only when both values ​​in the line are the same.

 return Validator::make($data, [ 'serial' => 'required|min:3|unique:cards,serial', 'number' => 'required|min:8|max:8|unique:cards,number|regex:/[0-9]{8,8}/', 'date_close' => 'required', ]); 

    1 answer 1

    There is such a regular opportunity:

     public function rules() { $currentId = $this->route()->parameter('table_name') ?: 'NULL'; $serial = (int)$this->serial ?: 'NULL'; return [ 'number' => 'unique:table_name,number,' // Для текущей записи проверку не делаем . $currentId . ',id,' // Уникальность в рамках серии . 'serial,' . $serial, ]; } 

    If for some reason it does not fit, then you can try using this package and the rule:

     'serial' => 'unique_with:your_table,cards'