php - 如何在独特的属性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 添加同一年份时不起作用
解决方案
尝试在验证规则中使用 where:
'year' =>['required', Rule::unique('charges')->where(function ($query) {
return $query->where('ac_id', $this->get('ac_id'));
})]
推荐阅读
- html - 是第 2 个
- git - GitLab:缺少新的组按钮
- javascript - 监听自定义事件但将代码设置在组件的 HTML 之外
- javascript - 如何从重定向的 url 获取查询数据
- html - 修复移动设备上的 CSS 宽度问题
- java - 在 Android 7+ 上使用 Glide 在片段 ImageView 中显示 GIF 图像
- angular - 在 Angular 中注入 activateRoute 类
- autohotkey - 如何设置“right-Alt + 。” 打印“>”
- c# - 多系列柱形图
- django - Django - 'QuerySet' 对象没有属性'künstler_set'