1

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

2
  • Your code should work. what is the error? Commented Jun 23, 2017 at 7:58
  • No it's not working. Commented Jun 23, 2017 at 8:33

1 Answer 1

2

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));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.