0
votes

I am trying to add more users with the same email in the database disabling the unique for email, adding another column for that [account]...

The registration form also includes this field,

<input type="text" name="account" required hidden value="{{getUUid()}}"/>
@if ($errors->has('account'))
   <span class="help-block">
   <strong>{{ $errors->first('account') }}</strong>
</span>
@endif

and all is stored correctly in the database, with multiple users with same email. The problem I am facing is related to the validation; changed RegisterController.php to

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255',
        'password' => 'required|string|min:6|confirmed',
        'account' => 'required|unique:users|max:50',
    ]);
}

and create function to

protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'account' => (getEvent()->hash.'_'.$data['email']),           

    ]);

}

when actually there is a duplicate I am having an error 500 with the database error

Illuminate \ Database \ QueryException (23000)
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry for key 'users_account_unique'

I am new to Laravel and don't understand what else I am missing to perform the same behavior of the email validation and prevent the database error.

* Solved * with a middleware redirecting to register if the user already exists.

1
Have you updated the email column to not be a unique column in your migration?Classified
The column that has an integrity violation is the account field, the email is not unique and actually can register multiple times same email.daniel
(getEvent()->hash.'_'.$data['email']) may be it's producing duplicate accountSohel0415
It is in fact but how can I handle the validator to give to the user some message like the email was?daniel

1 Answers

1
votes

Following line is causing you the problem as it's producing duplicate result-

(getEvent()->hash.'_'.$data['email'])

You can't validate account field like form validation as it is not coming from form data, it is generated later. But you can check this manually-

$user = User::where('account','=',(getEvent()->hash.'_'.$data['email']))->first();
if($user!=null){
     return back()->withInput()->with('errorMessage','Account already Exists!!');
}
return User::create([
    'name' => $data['name'],
    'email' => $data['email'],
    'password' => bcrypt($data['password']),
    'account' => (getEvent()->hash.'_'.$data['email']),           

]);