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

lat4732's avatar
Level 12

Problem with email uniqueness on user registration

Hey!

As I'm not that good in explanations just have a look at this

visualization

That's a list of registered users. As you can see a user is able to register with the same email but with changed uppercase letters. How can I avoid this?

0 likes
21 replies
Sinnbeck's avatar

What is the database type? Mysql is case insensitive out of the box, so maybe someone changed it by accident?

I assume the screenshot is from production?

lat4732's avatar
Level 12

@Sinnbeck It's just me playing around with the database. I haven't changed anything.

The email column type in the users table is

character varying(255) NOT NULL

No, the app is not in production yet.

Sinnbeck's avatar

@Laralex Ok. It might be related to the mysql running on your system. Windows? How is it installed?

It is also possible that you didnt set the column to unique in the migration? Just tested in my own database by duplicating username (which is unique)

Error synchronizing data with database

Reason:
SQL Error [1062] [23000]: (conn=116) Duplicate entry 'FOO' for key 'users_username_unique'
lat4732's avatar
Level 12

@Sinnbeck I'm running PostgreSQL. The app is on a live server. I haven't forgot unique() in the users table migration file.

$table->string('email')->unique();
Sinnbeck's avatar

@Laralex Ah postgresSQL might be different :) I havent been able to find any articles regarding it, but my guess is that you should be able to make it case insensitive somehow. Maybe someone who actually uses postgres can answer better (I only use mysql, mariadb and mssql)

1 like
Sinnbeck's avatar

Suggestion from @tray2

Use a mutator/accessor to always ensure the email is lowercase.

1 like
lat4732's avatar
Level 12

@Sinnbeck Well, okay.

User Model

public function getEmailAttribute($value) {
        return strtolower($value);
}

But how is that gonna help me to prevent users to register with the same email with changed uppercase letters?

lat4732's avatar
Level 12

@okusax app/Actions/Fortify/CreateNewUser.php

User::create([
       'name' => $input['name'],
       'email' => $input['email'],
       'password' => Hash::make($input['password'])
]);
Sinnbeck's avatar

@Laralex so this should make sure they are always lowercase :)

User::create([
       'name' => $input['name'],
       'email' => strtolower($input['email']),
       'password' => Hash::make($input['password'])
]);
1 like
okusax's avatar
okusax
Best Answer
Level 6

@Laralex

define:

// User.php

public function setEmailAttribute($value)
{
    $this->attributes['email'] = strtolower($value);
}

every time you assign the user email, it will be lowercased:

$user = new User();
$user->name = $input['name'];
$user->password = Hash::make($input['password']);
$user->email = $input['email']; // will be converted to [email protected] on the database
$user->save(); // when saved of course

if you use create() I think you should do the conversion manually:

User::create([
       'name' => $input['name'],
       'email' => strtolower($input['email']),
       'password' => Hash::make($input['password'])
]);

There is an old topic about this issue ( https://github.com/laravel/framework/issues/9430 ) Some people manage the unique validation in a Request Validation (ensuring that always your laravel application would be dealing with a lowercase email: https://github.com/laravel/framework/issues/9430#issuecomment-274482274 )

Others recommends to use a middleware to alter that kind of data to lowercase if detected https://github.com/laravel/framework/issues/9430#issuecomment-354729914

Maybe you can try that options too and check what works best for you.

lat4732's avatar
Level 12

@okusax Okay, nice. But how do I make this message

visualization2

appear like this

visualization2

(as a part of $errors variable, something like putting this validation in the controller's $request->validate() method)

Shaden's avatar

I think you should intercept the request before validation and make sure it is passed to validation in lower case

 $request->email = strtolower($request->email);

$request->validate .....
1 like
lat4732's avatar
Level 12

Hey!

Sorry for replying on this old topic, but I have a problem with the login now. When a user registers with [email protected] and tries to login with [email protected] leads to an error "These credentials do not match our records.". How do I deal with that? It's because I lowercase all the emails the email on user registration and in the database the email is with lowercase letters. How to access LoginController and add strtolower($request->email) before validation?

Please or to participate in this conversation.