Oct 8, 2019
7
Level 4
Rule::exists Validation of Array
Hey Everyone, I have a requirement to validate a request attribute array contains values that exist in a database table. The submitted array is an array of objects, i can use the id attribute of each object to check that id exists on my database tabel. I think Rule::exists might be suitable to meet the requirement, but I'm unclear how to get access to the submitted values in the array to validate them. What I have so far is below, can you recommend how this requirement can be best solved?
// excerpt of validation rules
'cities' => 'required|array',
'cities.*' => [
Rule::exists('cities')->where(function ($query) {
// i woulld like something like
$query->where('id', $array_item->id)
// but i can't find how i access the array_item
}),
],
Level 7
'cities' => 'required|array',
'cities.*.id' => 'exists:cities,id'
2 likes
Please or to participate in this conversation.