对数组进行验证时,Laravel对两列进行唯一验证



我可以通过自定义这样的where查询来验证两个表列的唯一性。

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

在验证数据数组时,我将如何执行类似的操作?

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

2022更新

Laravel 9现在有了解决这个确切问题的功能。

https://laravel.com/docs/9.x/validation#accessing-嵌套数组数据


最后得到了以下内容,尽管我希望有一种方法可以从函数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;
})
]

最新更新