首页 > 解决方案 > 如何在独特的属性laravel中添加多个条件?

问题描述

表:收费

id | ac_id | year |
| - | -- |-------------- |
| 1 | 1  | 2021          |
| 2 | 1  | 2020          |
| 3 | 2  | 2021          |
| 4 | 2  | 2020          |

我在进行验证时遇到问题,就 ac_id 而言,年份应该是唯一的

 'year'=>"required|unique:charges,year",

我尝试了这个验证规则,但在为不同的 ac_id 添加同一年份时不起作用

标签: phplaravelvalidation

解决方案


尝试在验证规则中使用 where:

 'year' =>['required', Rule::unique('charges')->where(function ($query) {
                    return $query->where('ac_id', $this->get('ac_id'));
    })]

推荐阅读