PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 6, 2022

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

 February 06, 2022     laravel, php     No comments   

Issue

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?


Solution

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.



Answered By - SuperDJ
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing