How to check multiple values ​​with one field in laravel validation

I have an array of id and I want to check each id with a database table using the laravel validator as I can check multiple id

My code is 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 want to check that each id exists in a normal schedule table? so how can i check this array id

0


source to share


1 answer


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));

      

+1


source







All Articles