Pouya Parsaei
Pouya Parsaei

Reputation: 57

How to check if email exists only when the email is verified in Laravel?

In Laravel 8 I have this validation in RegisterRequest:

public function rules()
{
    return [
        'name' =>'required|max:255',
        'email' =>'required|email|max:255|unique:users,email',
        'mobile' =>'required|digits:11|unique:users,mobile',
        'password' =>'required|confirmed|min:8|max:255'
    ];
}

The problem is that maybe the user entered the wrong email or maybe they entered someone else's email. I want to check if email exists only when the email is verified. How to do that?

Upvotes: 0

Views: 790

Answers (2)

SuperDJ
SuperDJ

Reputation: 7661

You could use

Rule::unique((new User)->getTable(), 'email')->whereNotNull('email_verified_at')

instead of unique:users,email.

(new User)->getTable() will return the User model's table. Using Rule:: you are able to query the database and therefor you are able to check if email_verified_at is empty or not. Here you can read more about using Rule::unique.

Upvotes: 1

elmahdi bouzouggar
elmahdi bouzouggar

Reputation: 46

Rule::unique('users','email')->where('email_verified_at')

Upvotes: 0

Related Questions