Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GrahamMorbyDev's avatar

"message": "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'users_email_unique'

Hey so I have a login module that is suddenly giving me an error

"message": "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'users_email_unique' (SQL: insert into `users` (`updated_at`, `created_at`) values (2018-10-11 13:17:54, 2018-10-11 13:17:54))",

the login code looks like

 //The createToken method is one of the methods Laravel Passport adds to the user model.
    public function login(Request $request) {

        if(Auth::attempt(['email' => $request->get('email'), 'password' => $request->get('password')])) {
            $status = 200;
            $response = [
                'user' => Auth::user(),
                'token' => Auth::user()->create(['bigStore'])->accessToken,
            ];
        return response()->json($response, $status);
        }else {
            $status = 401;
            $response = ['error' => 'Unauthorised'];
            return response()->json($response, $status);
        }

    }

and the front end is

handleSubmit(e) {
                e.preventDefault();
                if (this.password.length > 0) {
                    let email = this.email;
                    let password = this.password;

                    axios.post('api/login', {email, password}).then(response => {
                        let user = response.data.user;
                        let is_admin = user.is_admin;

                        localStorage.setItem('bigStore.user', JSON.stringify(user));
                        localStorage.setItem('bigStore.jwt', response.data.token);

                        if (localStorage.getItem('bigStore.jwt') != null) {
                            this.$emit('loggedIn');
                            if (this.$route.params.nextUrl != null) {
                                this.$router.push(this.$route.params.nextUrl)
                            } else {
                                this.$router.push((is_admin === 1 ? 'admin' : 'dashboard'))
                            }
                        }
                    });
                }
            }
0 likes
3 replies
D9705996's avatar

You have a unique constraint on your email column and trying to insert an empty string into this field but another record exists with an empty string email so the database is throw the error

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'users_email_unique'

You should review the validation documentation to prevent this sort of issue gracefully before you hit the database

https://laravel.com/docs/5.7/validation

GrahamMorbyDev's avatar

I don't get why it's trying to update the email?? I'm simply asking it to log in and create a token?? Or did I do something wrong

D9705996's avatar

The error shows your trying to insert into users (nog update)

insert into users

Which is from this line

Auth::user()->create(['bigStore'])->accessToken,

I suspect your email field in you migrations is unique and nullable.

Please or to participate in this conversation.