【问题标题】:Can I check a unique record, except for the edited one AND except for those that have status = 0, in Laravel validator?我可以在 Laravel 验证器中检查唯一记录,除了已编辑的记录和状态 = 0 的记录吗?
【发布时间】:2021-03-13 20:11:13
【问题描述】:

我可以检查一条唯一记录,除了已编辑的记录和状态 = 0 的记录除外吗?

有点像:

return [
    'contract' => [
        'required',
        'integer',
        Rule::unique('table')
           ->ignore($this->id)
           ->where('contract', $this->contract)
           ->andWhere('status', $this->plan_contract)],           
  /**SOMETHING MORE**/
];
}

【问题讨论】:

    标签: php laravel validation


    【解决方案1】:

    根据official docs,可以。

    但是你应该稍微重写一下你的代码:

    return [
        'contract' => [
            'required',
            'integer',
            Rule::unique('table')->ignore($this->id)->where(function ($query) {
                return $query
                    ->where('contract', $this->contract)
                    ->where('status', $this->plan_contract)
            }),
        ];
    ];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-23
      • 1970-01-01
      • 2013-07-18
      • 2018-06-05
      • 1970-01-01
      • 1970-01-01
      • 2018-04-24
      • 1970-01-01
      相关资源
      最近更新 更多