18

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.

2
  • Which version of laravel are you using? Commented Sep 13, 2018 at 11:45
  • Laravel version 5.6 Commented Sep 15, 2018 at 4:41

1 Answer 1

50

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());
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.