paul-a-byford
1 week ago
1149
5
Requests

Rule::exists Validation of Array

Posted 1 week ago by paul-a-byford

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
        }),
      ],

Please sign in or create an account to participate in this conversation.