How to validate multiple value with single field in laravel validation

问题

我有一个 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');
}

我想验证routine_schedule 表中是否存在每个id? 那么我如何验证这个数组 id

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

更多相关内容:请点击查看