【发布时间】:2013-07-22 23:29:10
【问题描述】:
在 laravel 中,当一个新用户注册到我的网站并且他们使用的电子邮件已经存在于数据库中时。如何告诉用户该电子邮件已经存在?我是 laravel 框架的新手。一个示例代码也很好。
【问题讨论】:
-
在编写查询之前...搜索现成的解决方案总是好的。好问题 +1
在 laravel 中,当一个新用户注册到我的网站并且他们使用的电子邮件已经存在于数据库中时。如何告诉用户该电子邮件已经存在?我是 laravel 框架的新手。一个示例代码也很好。
【问题讨论】:
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 中了解有关验证的更多信息以及您可以用它做什么。
【讨论】:
'required|unique:users, email'会导致SQL语句失败。
唯一规则的基本用法
'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'
];
您可以向 $rules 添加更多规则,例如:
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);
}
【讨论】:
if(sizeof(Users::where('email','=',Input::get('email'))->get()) > 0) return 'Error : User email exists';
【讨论】:
伟大的资源只有 Laravel 文档@ 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);
}
【讨论】:
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();
}
我认为当你尝试这样的事情时,你会使用模型获得顺利的检查
【讨论】:
我们可以使用验证器。
在您的控制器中。
$validator = $request->validate([
'name' => 'required',
'phone' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required',
]);
可见
@error('email') <span class="text-danger error">{{ $message }}</span>@enderror
【讨论】: