Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jaythanki's avatar

multiple tables with multiple columns validation for unique values

  • table 1
  • table 2 have id,code,table_1_id
  • table 3 have id,table_1_id,table_2_id,code

i want to create custom validation in laravel to match each value while creating table 2 values and table_1_id (array) it can be multiple

code should be unique with combination of table_1_id , table_2_id and code

it should match with table 2 and in table 3 for unique combination of ids and code

0 likes
2 replies
jaythanki's avatar

already created custom validation for the same but i want to pass array in custom validation from model becuase i want to check each combination of unique values.

//Service Provider 
Validator::extend('unique_code', function($attribute, $data, $parameters,$validator ) {
// my code for checking unique values 
});

//model of table 2
// i want to pass array of ids from my controller which is had foreign key of table 1 to check with code in table 2
public static function validate($data) {
  $rule = array(
      'code' => 'required|max:255|unique_code:' . array(all ids). '|unique:table2,code,NULL,id,table_1_id,' . array(all ids),
    );
    $messages = array(
      'unique' => ':attribute has already been taken.',
    );
    $data = Validator::make($data, $rule, $messages);
    $data->setAttributeNames(array(
      'code' => ucfirst(SYS_CODE)
    ));
    return $data;
  }

above code i had tried but can't pass array from model to service provider in $parameters

Please or to participate in this conversation.