Reputation: 271
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
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