Search code examples
phplaravellaravel-validation

How to validate array by themselves? (laravel)


I have an array of data for example in which they have the same id.

[
    {
        id: 222,
        name: 'Fff',
    }
    {
        id: 223,
        name: 'Ssss',
    },
    {
        id: 222,
        name: Wwww',
    }
]

From the data above, the expected behavior is, it should fail because they have the same id.

Is there a Laravel built-in validation in which we can validate this scenario?


Solution

  • Yes there is a validation called distinct to check for duplicate values. For more info https://laravel.com/docs/7.x/validation#rule-distinct

    'foo.*.id' => 'distinct'