Laravel:在更新操作时,为关联阵列输入请求添加唯一的验证规则

发布于 2025-02-13 22:21:34 字数 605 浏览 0 评论 0原文

我正在执行更新操作,并具有下面的输入请求:

array:1 [
  "buildings" => array:3 [
    0 => array:4 [
      "item_id" => 6
      "florida_number" => "1006"
      "serial_number" => "1002"
      "building_id" => 6
    ]
    1 => array:4 [
      "item_id" => 7
      "florida_number" => "1007"
      "serial_number" => "1002"
      "building_id" => 7
    ],
    .....
    .....
  ]
]

现在我想在 serial_number 字段上应用唯一的验证规则,该规则存储在 项目< /strong> 数据库中的表。
我需要照顾的是在检查唯一性时忽略当前项目ID。

I am doing update operation and have an input request like below:

array:1 [
  "buildings" => array:3 [
    0 => array:4 [
      "item_id" => 6
      "florida_number" => "1006"
      "serial_number" => "1002"
      "building_id" => 6
    ]
    1 => array:4 [
      "item_id" => 7
      "florida_number" => "1007"
      "serial_number" => "1002"
      "building_id" => 7
    ],
    .....
    .....
  ]
]

Now I want to apply unique validation rule on serial_number field which is stored in items table in database.
The thing I need to take care of here is to ignore the current item id while checking uniqueness.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是我的挚爱i 2025-02-20 22:21:35

最简单的解决方案是将以下规则添加到您的表格请求中:

return [
            'buildings' => 'array',
            'buildings.*' => Rule::forEach(function ($value, $attribute) {
                return [
                    'serial_number' => 'unique:items,serial_number,'. $value['item_id']
                ];
            })
      ];

The easiest solution is to add a rule like below to your form request:

return [
            'buildings' => 'array',
            'buildings.*' => Rule::forEach(function ($value, $attribute) {
                return [
                    'serial_number' => 'unique:items,serial_number,'. $value['item_id']
                ];
            })
      ];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文