Home > Software engineering >  Laravel unique validation of two columns when validating against an array
Laravel unique validation of two columns when validating against an array

Time:02-06

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

CodePudding user response:

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;
    })
]
  •  Tags:  
  • Related