Tester
Tester

Reputation: 271

How to validate multiple value with single field in laravel validation

I have array of id and i want to validate each id with database table using laravel validator so how can i validate multiple id

My code like this

$idArray = [10,15,16]; // I have tree routine_schedule table id 
$validatior = Validator::make(array("id"=>$idArray), ["id"=>"required|exists:routine_schedule,id"]);
if ($validatior->passes()){
    exit('valid');
}else{
    exit('invalid');
}

I wan to validate each and every id is exist in routine_schedule table? so how can i validate this array id's

Upvotes: 1

Views: 2418

Answers (1)

aaron0207
aaron0207

Reputation: 2333

Try something like this:

public function rules($idArray)
{
  $rules = [];

  foreach($idArray as $key => $val)
  {
    $rules[$key] = 'required|exists:routine_schedule,id';
  }

  return $rules;
}

$validatior = Validator::make($idArray, rules($idArray));

Upvotes: 2

Related Questions