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

时间:2013-07-22 23:29:11

标签: php laravel

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

6 个答案:

答案 0 :(得分:35)

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

您可以在Laravel Docs中了解有关验证的更多信息以及您可以采取的措施。

答案 1 :(得分:7)

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

答案 2 :(得分:7)

唯一规则的基本用法

'email' => 'unique:users'

指定自定义列名

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

强制使用唯一规则忽略给定ID

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

添加其他Where子句

您还可以指定更多条件,将其添加为查询的“where”子句:

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

上述内容来自the documentation of Laravel

您可以添加:

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

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

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

它会自动生成错误消息

并添加:

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

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

到模型 User.php

然后在您用于注册的功能中,您可以添加:

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

答案 3 :(得分:3)

很棒的资源只有Laravel Documentation @ enter link description here

我在集成用户管理系统

时也是如此
 $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);
    }

答案 4 :(得分:2)

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

我想当你尝试这样的事情时,你可以使用Model

进行顺利检查

答案 5 :(得分:0)

我们可以使用验证器。

在您的控制器中。

$validator =  $request->validate([
            'name' => 'required',
            'phone' => 'required',
            'email' => 'required|email|unique:users',
            'password' => 'required',
        ]);

       

在视图中

  @error('email') <span class="text-danger error">{{ $message }}</span>@enderror