首页 > 解决方案 > Laravel array validation for unique attribute in array but not required to be unique in table

问题描述

I have an array in php. i need to validate array such that each abc_id should be unique in array but not required to be unique in database table.

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

Thanks in Advance.

标签: phplaravelvalidation

解决方案


您可以使用不同的 laravelarray验证规则。

$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());
    

推荐阅读