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

LaraBABA's avatar

Unique email not working on validator

Hello,

The same email can register again and again on my platform. I am not understanding as I added this rule:

$validator = Validator::make(
            $request->all(), [
                'name' => ['required', 'string', 'max:255'],
                'surname' => ['required', 'string', 'max:255'],
                'email' => ['required', 'unique:users', 'email'],
            ],
            $messages = [
                'required' => 'The :attribute field is required.',
            ]
        );

The table is called "users" and the email field "email"

Any idea why please?

Thanks

0 likes
11 replies
Jaytee's avatar

It should work as it is, but you can update the validation rule and be explicit, incase laravel isn't picking it up.

unique:users,email

However, if you're using PostgreSQL or another database that is case-sensitive, this may be the reason why. You'll need to convert the email into lowercase and always check the value as lowercase. MySQL is not case-sensitive, so normal validation is fine.

3 likes
asimraza's avatar

'email' => ['required', 'unique:users', 'email', 'unique:users,email' . $user->id],

LaraBABA's avatar

Thank you for the reply. It is already case sensitive as I have added a middleware for it.

LaraBABA's avatar

Interresting.....

So if this is the first registration......I need to first check on the user model the requested email and then get its id. But...the problem is, if the user is not registered yes, the $user->id will return nothing. Will this not cause an issue? 'email' => ['required', 'unique:users', 'email', 'unique:users,email' . $user->id],

Thank you

Jaytee's avatar

What does the middleware do exactly? Middleware is used with routing/requests, not for validation, therefore it'll be useless.

Also, remove the 'unique:users,email' . $user->id because that doesn't make sense. You can't have a user if you're registering. You just want unique:users,email. Ignoring the user id is for a completely different scenario.

itxrahulsingh's avatar

First, you check you have Included your validator Facade if not include this,

     use Illuminate\Support\Facades\Validator;

and then replace the below code to yours,

      'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
Jaytee's avatar

That wouldn't be the cause. PHP would have errored due to an incorrect import.

Based on the information provided, OP is using a case-sensitive database, which will likely be the cause.

Snapey's avatar

You can also check the database request and make sure it is querying for a match on the correct column

Snapey's avatar

As you have manually created the validator, are you actually checking if it fails?


        if ($validator->fails()) {
            return redirect('whereever')
                        ->withErrors($validator)
                        ->withInput();
        }

Are other validation errors picked up apart from email address?

siangboon's avatar
'email' => ['required', 'unique:users,email', 'email'],

by the way, you should set the email column as unique in database as well.

Amirho3ein's avatar

change 'email' => ['required', ' unique : users , email ' , ' email '], to 'email' => ['required', ' unique : App\Models\User , email ' , ' email '], <<<<<<

1 like

Please or to participate in this conversation.