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

Time:01-27

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?

CodePudding user response:

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

CodePudding user response:

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.

  •  Tags:  
  • Related