Laravel Unique Validation Based on Multiple Column Data Value

Viewed 984

Image of Database

In this scenario , My Phone number will be unique based on my domain_id. Phone number can be multiple in phone columns but its obvioulsy would be depend on domain_id column.

Example: 01303062727 This Numbers are two times is phone column but they have different domain_id value.

How can be validate this rules ?

1 Answers

here you can take advantage of unique rule that laravel provides. your code would be something like this :

'phone_number' => Rule::unique('users')->where(function ($query) {
    return $query->where('domain_id', $domain_id);
})

obviously you may pass $domain_id variable along with your inputs or any other way you want . this code will scopes the query to make rule of uniqueness only toward records that have domain_id of a specific value. so repeated number will be allowed but for different domain ids

Related