I have an items array, sent to the controller as JSON within the items form key.
[{
"sku": "w-hook-as1",
"qty": 2,
"cost_ex_tax": "34.22",
"tax_rate": "0.2"
}]
Here it is converted to an array:
$request->merge(['items' => json_decode($request->items, true)]);
and validated against the rule:
'items.*.sku' =>[
'required',
Rule::exists('products')->where(function ($query) {
$query->where('company_id', Auth::user()->company_id);
})
],
The exists rule does work, if the array key that its validation against exists, assuming the key exists. If I simply send:
[{
"qty": 2,
"cost_ex_tax": "34.22",
"tax_rate": "0.2"
}]
Then the validation passes.
Is there a way I can check that the key exists as well as validate its content? I would have expected that the required check does this, but it does not seem to work.
How to validate array in Laravel? - This answer suggests to validate that it is an array with x number of elements, but still does not check the exact keys I am looking for are there.