0

I can validate two table columns for uniqueness by customizing the where query like this.

'email' => [
    'required',
    Rule::unique('users')->where(function ($query) {
        return $query->where('account_id', $this->request('account_id'));
    })
]

How would i do a similar thing when validating an array of data?

'email.*' => [
    'required',
    Rule::unique('users')->where(function ($query) {
        // How can i customize the query per array element?
    })
]
Mike Mellor
  • 1,316
  • 17
  • 22

1 Answers1

0

2022 Update

Laravel 9 now has functionality for this exact problem.

https://laravel.com/docs/9.x/validation#accessing-nested-array-data


Ended up with the following, though i was hoping there was a way to access the current index of the currently validated array element from within the function params themself.

$i = 0;

'email.*' => [
    'required',
    Rule::unique('users')->where(function ($query) use (&$i) {
        return $query->where('account_id', $this->request('account_id')[$i]);
        $i += 1;
    })
]
Mike Mellor
  • 1,316
  • 17
  • 22