Array keys validation (check that IDs exists in database)

Viewed 40

I have to validate an array where the keys are IDs, like this:

array:1 [
  "data" => array:3 [
    13 => array:1 [
      "price" => 100
    ]
    14 => array:1 [
      "price" => 200
    ]
    46 => array:1 [
      "price" => 300
    ]
  ]
]

So 13, 14, and 46 are IDs. I validate the data array and the price like this:

$request->validate([
    'data' => ['required', 'array', 'min:1'],
    'data.*.price' => ['required', 'numeric'],
]);

But I don't know how to validate the keys, saying something like 'data.*' => ['exists:table,id'].

Is it possible?

0 Answers
Related