3

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 ?

4
  • Can't you just pull up the database with the phone number in question, before inserting it into the database, and either accept or reject it based on the result? Commented Jan 30, 2022 at 7:48
  • Actually I'm doing this right now! @KevinY Commented Jan 30, 2022 at 8:23
  • Just create a custom validation rule (see laravel.com/docs/8.x/validation#custom-validation-rules), and do your logic here. What is the problem exactly ? Commented Jan 30, 2022 at 9:10
  • Somehow, I solve this problem by doing this, Rule::unique('customers', 'phone')->where(function ($query) { $query->where('phone', $this->phone) ->where('domain_id', request()->get('domain_id')); })->ignore($this->id) Commented Jan 30, 2022 at 9:59

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I did this in way. @AlirezaSalehi Rule::unique('customers', 'phone')->where(function ($query) { $query->where('phone', $this->phone) ->where('domain_id', request()->get('domain_id')); })->ignore($this->id)

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.