Laravel 验证数组时两列的唯一验证
Laravel unique validation of two columns when validating against an array
我可以像这样自定义 where 查询来验证两个 table 列的唯一性。
'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-nested-array-data
最后得到以下结果,尽管我希望有一种方法可以从函数参数本身访问当前验证的数组元素的当前索引。
$i = 0;
'email.*' => [
'required',
Rule::unique('users')->where(function ($query) use (&$i) {
return $query->where('account_id', $this->request('account_id')[$i]);
$i += 1;
})
]
我可以像这样自定义 where 查询来验证两个 table 列的唯一性。
'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-nested-array-data
最后得到以下结果,尽管我希望有一种方法可以从函数参数本身访问当前验证的数组元素的当前索引。
$i = 0;
'email.*' => [
'required',
Rule::unique('users')->where(function ($query) use (&$i) {
return $query->where('account_id', $this->request('account_id')[$i]);
$i += 1;
})
]