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

时间:2022-09-25 23:50:59

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框架的新手。示例代码也不错。

5 个解决方案

#1


35  

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中了解有关验证的更多信息以及您可以使用它做些什么。

#2


6  

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

#3


6  

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:

您可以在$规则中添加更多规则,例如:

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);
}

#4


3  

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

优秀的资源只有Laravel Documentation @输入链接描述

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);
    }

#5


1  

 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

我想当你尝试这样的事情时,你可以使用模型进行顺利检查

#1


35  

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中了解有关验证的更多信息以及您可以使用它做些什么。

#2


6  

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

#3


6  

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:

您可以在$规则中添加更多规则,例如:

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);
}

#4


3  

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

优秀的资源只有Laravel Documentation @输入链接描述

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);
    }

#5


1  

 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

我想当你尝试这样的事情时,你可以使用模型进行顺利检查