Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 591
Neha Avatar asked Sep 13 '18 11:09

Neha


1 Answers

You can use distinct rule of laravel array validation.

$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());
    
like image 145
Mayank Majithya Avatar answered Nov 09 '22 23:11

Mayank Majithya