I
I
Ilya78rus2018-11-28 23:35:00
MySQL
Ilya78rus, 2018-11-28 23:35:00

Duplicate entry '[email protected]' for key 'users_email_unique'?

Laravel 5.5 + AjaxRegister When registering users, sometimes I come across the following error in the logs:

at /var/www/game2/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664, PDOException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '[email protected]' for key 'users_email_unique' at /var/www/game/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458)

According to users, when registering, they are thrown on a white screen and that's it. When the page is updated, they are registered, but this situation scares away a lot of people. Again, the problem occurs once in a while.
RegisterController code:
use RegistersUsers;

protected $redirectTo = '/';

public function __construct()
{
    $this->middleware('guest');
}

protected function validator(array $data)
{
    $data['email'] = $data['email_reg'];
    $data['password'] = $data['password_reg'];
    $vaild = Validator::make($data, [
        'name' => 'required|max:255|unique:users',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|min:6|confirmed',
    ]);

    if ($vaild->fails()) {
        return json_encode($vaild->errors());
    }

    return true;
}

public function register(Request $request)
{
    $v = $this->validator($request->all());

    if (! is_bool($v)) {
        return $v;
    }

    event(new Registered($user = $this->create($request->all())));

    $this->guard()->login($user);

    return $this->registered($request, $user)
        ?: redirect($this->redirectPath());
}

protected function create(array $data)
{
    $data['email'] = $data['email_reg'];
    $data['password'] = $data['password_reg'];

    $avatar = Avatars::inRandomOrder()->first();

    $referred_by = Cookie::get('referral');
    $come_url = NULL;
    if(null !== Cookie::get('come_url')){
        $come_url = Cookie::get('come_url');
    }

     return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'come_url' => $come_url,
            'last_login' => Carbon::now(),
            'avatar' => $avatar->img,
            'uniq' => md5($data['email'].$data['name'])
        ]);
}

protected function registered(Request $request, $user)
{
    return "true";
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
U
Uber Noob, 2018-11-29
@ubernoob

Somewhere a test e-mail or a default e-mail [email protected] is sewn up. When some kind of verification fails or it is not specified, then it is substituted. But it already exists in the database and this field has a unique index so that there are no 2 identical e-mail addresses.
The output is stupid: remove the unique index from the field.
The output is normal: search for the project by searching for [email protected] and see where it comes from

T
Tesla, 2018-11-29
@Tesla

public function register(Request $request)
{
    $v = $this->validator($request->all());

Someone does not read the documentation? FormRequest
return $this->registered($request, $user)
        ?: redirect($this->redirectPath());

How is the action [email protected]called? Ajax or regular request? And that is, there is a suspicion that users see a white screen with the inscription "true"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question