[英]Laravel 5.1 SQLSTATE[23000]: Integrity constraint violation error
On registering with new email, I can successfully register the new user, but when I try to make a validation rule
to restrict an existing email from signup, instead of giving validation error message, its giving me Integrity constraint violation error
, 在注册新电子邮件时,我可以成功注册新用户,但是当我尝试制定
validation rule
以限制现有电子邮件无法注册时,而不是给出验证错误消息,它给了我Integrity constraint violation error
,
here is my controller
这是我的
controller
public function postRegister(Request $request){
$v = validator::make($request->all(), [
'first_name' => 'required|max:30',
'last_name' => 'required|max:30',
'email' => 'required|unique:users|email',
'mobile' => 'required|min:10',
'password' => 'required|min:8',
'confirm_password' => 'required|same:password'
]);
if ($v->fails())
{
return redirect()->back()
->withErrors($v->errors())
->withInput(Input::except('password', 'confirm_password'));
}
else{
// validation successful ---------------------------
/*
add data to database
*/
} }
and this is my migration
这是我的
migration
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name', 80);
$table->string('last_name', 80);
$table->string('email')->unique();
$table->string('mobile',80);
$table->string('password', 60);
$table->boolean('confirmed')->default(0);
$table->string('confirmation_code')->nullable();
$table->float('amount', 80);
$table->rememberToken();
$table->timestamps();
});
}
How can I fix this error? 如何解决此错误? Any help appreciated.
任何帮助表示赞赏。
Should be: 应该:
if ($v->fails())
{
return redirect()->back()
->withErrors($v->messages()) //Not errors
->withInput(Input::except('password', 'confirm_password'));
}
If you read the documentation of Validators. 如果您阅读了验证程序的文档。
First Parameter: Is for Inputs 第一个参数:用于输入
Second Parameter: Is for Rules 第二个参数:用于规则
Third Parameter: Is for custom Messages 第三个参数:用于自定义消息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.