【问题标题】:Laravel unique validation of two columns when validating against an arrayLaravel 对数组进行验证时对两列的唯一验证
【发布时间】:2022-02-05 18:11:10
【问题描述】:

我可以通过像这样自定义 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?
    })
]

【问题讨论】:

标签: laravel validation


【解决方案1】:

结束了以下内容,尽管我希望有一种方法可以从函数参数本身访问当前验证的数组元素的当前索引。

$i = 0;

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

【讨论】:

    猜你喜欢
    • 2018-10-25
    • 2019-05-02
    • 1970-01-01
    • 2021-09-11
    • 2012-06-20
    • 1970-01-01
    • 2022-01-12
    • 2014-11-01
    • 1970-01-01
    相关资源
    最近更新 更多