我有id数组,我想使用laravel验证器验证每个id与数据库表,所以如何验证多个id
我的代码就像这样
$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');
}
我想验证每个id是否存在于routine_schedule表中?那我怎么能验证这个数组id
答案 0 :(得分:1)
尝试这样的事情:
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));