skip to Main Content

I want to validate Email column and Phone No column as Unique. Phone no is submitted to the Database as a concatenation of calling code and phone no. Here is my code.

public function customRegistration(Request $request)
{
    $request->validate([
        'name' => 'required',
        'email' => 'required|email|unique:users',
        'country' => 'required',
        'phone_no' => 'required|unique:users',
        'password' => 'required',
    ]);

    $data = $request->all();
    $check = $this->create($data);

    return redirect('login')->with('message', 'Signing up successful !!!');
}


public function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'country' => $data['country'],
        'phone_no' => $data['calling_code'] . '-' . $data['phone_no'],
        'password' => Hash::make($data['password']),
    ]);
}

In above code email validation is working perfectly. But the same phone no can be submitted to the DB. The validation rule is not working on the phone no. I couldn’t find where the error is.
Where should I change to validate the phone no as unique?

2

Answers


  1. EDIT:

    (Thanks to @brombeer, I noticed you are inserting values by concatenating, while comparing the initial value with that of concatenated already in database, which means you did not enforce uniqueness in database)

    so, you can do it like the beautiful accepted answer or you can just go with a validating it like,

    'phone_number' => [
            'required',
            function ($attribute, $value, $fail) use ($request) {
                $callingCode = $request->input('calling_code');
                $phoneNumber = $callingCode. '-' . $value;
                $exists = User::where('phone_number', $phoneNumber)->exists();
    
                if ($exists) {
                    $fail('The ' . $attribute . ' has already been taken.');
                }
            },
        ],
    
    Login or Signup to reply.
  2. You may choose to create a Custom Validation Rule to validate the uniqueness of the phone number.

    php artisan make:rule UniquePhoneNumber
    

    app/Rules/UniquePhoneNumber.php

    <?php
     
    namespace AppRules;
     
    use Closure;
    use AppModelsUser;
    use IlluminateContractsValidationValidationRule;
     
    class UniquePhoneNumber implements ValidationRule
    {
        /**
         * Run the validation rule.
         */
        public function validate(string $attribute, mixed $value, Closure $fail): void
        {
            return User::where('phone_no', request('calling_code') . '-' . $value)
                        ->doesntExist();
        }
    }
    

    Then make use of the new Custom Validation Rule:

    use AppRulesUniquePhoneNumber;
    
    public function customRegistration(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'email' => 'required|email|unique:users',
            'country' => 'required',
            'phone_no' => ['required', new UniquePhoneNumber],
            'password' => 'required',
        ]);
    
        $data = $request->all();
        $check = $this->create($data);
    
        return redirect('login')->with('message', 'Signing up successful !!!');
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search