简体   繁体   中英

Laravel 5.5 validator

Is there a way in laravel on how I can choose an email that is stored in the database?

For example, if I want a unique email, you use "unique:users" like this:

$validator = Validator::make($request->all(), [
        'email' => 'required|unique:users',
        'password' => 'required'
        ]);

But what I want to know is if it can check if the given email is stored in the database or not. Is it possible to do this with the validator?

If you want to be sure it doesn't exist in DB, use the unique rule:

unique:table

If you want to be sure the record exists in DB, use the exists rule:

exists:table

You can use exists ( https://laravel.com/docs/5.5/validation#rule-exists ) key word as :

$validator = Validator::make($request->all(), [
    'email' => 'required|unique:users|exists:users',
    'password' => 'required'
]);

Or you can do something like that :

$validator = Validator::make($request->all(), [
    'email' => 'required|unique:users',
    'password' => 'required'
]);

if ($validator->fails()) {
    echo 'That email address is already registered';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM