【问题标题】:Laravel array validation for unique attribute in array but not required to be unique in tableLaravel 数组验证数组中的唯一属性,但不需要在表中是唯一的
【发布时间】:2019-02-18 03:29:25
【问题描述】:

我在 php 中有一个数组。我需要验证数组,使得每个 abc_id 在数组中应该是唯一的,但在数据库表中不需要是唯一的。

$validator = Validator::make($request->all(), [
 'tests.*.*.abc_id' => 'should not be same in array' 
 ]);

提前致谢。

【问题讨论】:

  • 你用的是哪个版本的 laravel?
  • Laravel 5.6 版

标签: php laravel validation


【解决方案1】:

你可以使用 laravel 的 distinct 规则 array 验证。

$validator = Validator::make(
          ['products' => 
            ['product_id' => 1, 'quantity' => 5],
            ['product_id' => 1, 'quantity' => 99],
            ['product_id' => 2, 'quantity' => 1],
          ],
          ['products.*.product_id' => 'distinct']
        );
        
        dd($validator->passes());
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-15
    • 2019-11-15
    • 2018-01-25
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    相关资源
    最近更新 更多