Answer the question
In order to leave comments, you need to log in
Laravel: How to check 2 fields for uniqueness when registering a user?
How to check 2 fields for uniqueness when registering a user?
The registration form contains 2 main unique fields (Phone number and email)
In the controller file
App\Http\Controllers\Auth\RegisterController.php
protected function validator(array $data){
$messages = [
'email.unique' => 'Данный email зарегестрирован',
'phone.unique' => 'Данный номер телефона зарегестрирован',
'required' => 'Поле объязательно для заполнения',
'accepted' => 'Вы не приняли условия соглашения',
];
$phone = $data['phone'];
$email = $data['email'];
$rules = [
'name' => ['required', 'string', 'max:255'],
'surname' => ['required', 'string', 'max:255'],
'phone' => [
'required',
Rule::unique('users')->where(function ($query) use($phone,$email) {
return $query->where('phone', $phone)
->where('email', $email);
}),
// Rule::unique('users')->ignore($user->id),
],
// 'phone' => ['required', 'string', 'max:255', 'unique:users'],
'email' => ['required', 'string', 'email', 'max:255'],
'password' => ['required', 'string', 'confirmed'],
'checkbox' => ['accepted'],
];
return Validator::make($data, $rules, $messages);
}
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '89008007060' for key 'users_phone_unique'
$table->string('phone')->unique();
$table->string('email')->unique();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question