php 如何检查用户电子邮件是否已存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17799148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:28:41  来源:igfitidea点击:

how to check if a user email already exist

phplaravel

提问by ballerz

In laravel, when a new user is registering to my site and the email they use already exist in the database. how can tell the user that the email already exist ?. I am new to laravel framework. A sample code would be nice too.

在 Laravel 中,当新用户注册到我的网站时,他们使用的电子邮件已存在于数据库中。如何告诉用户电子邮件已经存在?我是 Laravel 框架的新手。示例代码也很好。

回答by Ed Rands

The validation feature built into Laravel lets you check lots of things, including if a value already exists in the database. Here's an overly simplified version of what you need. In reality you'd probably want to redirect back to the view with the form and show some error messages.

Laravel 内置的验证功能可以让你检查很多事情,包括数据库中是否已经存在一个值。这是您需要的过于简化的版本。实际上,您可能希望使用表单重定向回视图并显示一些错误消息。

// Get the value from the form
$input['email'] = Input::get('email');

// Must not already exist in the `email` column of `users` table
$rules = array('email' => 'unique:users,email');

$validator = Validator::make($input, $rules);

if ($validator->fails()) {
    echo 'That email address is already registered. You sure you don\'t have an account?';
}
else {
    // Register the new user or whatever.
}

);

);

Laravel has built-in human readable error messages for all its validation. You can get an array of the these messages via: $validator->messages();

Laravel 为所有验证内置了人类可读的错误消息。您可以通过以下方式获取这些消息的数组:$validator->messages();

You can learn more about validation and what all you can do with it in the Laravel Docs.

你可以在Laravel Docs 中了解更多关于验证以及你可以用它做什么。

回答by Loko

Basic Usage Of Unique Rule

唯一规则的基本用法

'email' => 'unique:users'

Specifying A Custom Column Name

指定自定义列名

'email' => 'unique:users,email_address'

Forcing A Unique Rule To Ignore A Given ID

强制唯一规则忽略给定 ID

'email' => 'unique:users,email_address,10'

Adding Additional Where Clauses

添加额外的 Where 子句

You may also specify more conditions that will be added as "where" clauses to the query:

您还可以指定更多条件,这些条件将作为“where”子句添加到查询中:

'email' => 'unique:users,email_address,NULL,id,account_id,1'

The above is from the documentation of Laravel

以上来自Laravel 的文档

You could add:

你可以添加:

public static $rules = [
    'email' => 'unique:users,email'
];

You can add more rules to the $rules like:

您可以向 $rules 添加更多规则,例如:

public static $rules = [
        'email' => 'required|unique:users,email'
];

It will automatically produce the error messages

它会自动产生错误信息

and add:

并添加:

public static function isValid($data)
{
    $validation = Validator::make($data, static::$rules);

    if ($validation->passes())
    {
        return true;
    }
    static::$errors = $validation->messages();
    return false;
}

to the model User.php

到模型User.php

Then in the function you're using to register, you could add:

然后在您用来注册的函数中,您可以添加:

if ( ! User::isValid(Input::all()))
{
    return Redirect::back()->withInput()->withErrors(User::$errors);
}

回答by munzx

if(sizeof(Users::where('email','=',Input::get('email'))->get()) > 0) return 'Error : User email exists';

回答by Khan Aqib

The great resource is only Laravel Documentation @ enter link description here

伟大的资源只是 Laravel 文档@ 在此处输入链接描述

I also did like below when integrating user management system

我在集成用户管理系统时也喜欢下面

 $user = Input::get('username');
  $email = Input::get('email');

$validator = Validator::make(
        array(
            'username' => $user,
            'email' => $email
        ),
        array(
            'username' => 'required',
            'email' => 'required|email|unique:users'
        )
  );
  if ($validator->fails())
    {
        // The given data did not pass validation
        echo 'invalid credentials;';
        // we can also  return same page and then displaying in Bootstap Warning Well
    }
    else {
        // Register the new user or whatever.
        $user = new User;
        $user->email = Input::get('email');
       $user->username = Input::get('username');

        $user->password = Hash::make(Input::get('password'));
        $user->save();

        $theEmail = Input::get('email');
         // passing data to thanks view 
        return View::make('thanks')->With('displayEmail', $theEmail);
    }

回答by Delino

 public function userSignup(Request $request, User $data){
    # check user if match with database user
    $users = User::where('email', $request->email)->get();

    # check if email is more than 1
    if(sizeof($users) > 0){
        # tell user not to duplicate same email
        $msg = 'This user already signed up !';
        Session::flash('userExistError', $msg);
        return back();
    }

    // create new files
    $data = new User;
    $data->name = $request->name;
    $data->email = $request->email;
    $data->password = md5($request->password);
    $data->save();

    //return back
    Session::flash('status', 'Thanks, you have successfully signup'); 
    Session::flash('name', $request->name);

    # after every logic redirect back
    return back();
}

I think when u try something like this you earn a smooth check using Model

我认为当你尝试这样的事情时,你可以使用模型获得顺利支票