Laravel 4:避免重复进入 [英] Laravel 4: avoid duplicate entry
问题描述
如果我输入新的电子邮件,所有的它工作正常
如果我输入数据库中已经存在的电子邮件,我会收到错误SQLSTATE [23000]:完整性约束违规:1062重复条目
因为我已经将该字段定义为数据库中的唯一。
所有我想做的是重定向:: back() - >与('message','电子邮件已经注册')
但我不知道我该怎么办?
我可以在方法控制器中放一个if语句?
或者我必须在模型中的$ rules中定义它,添加另一个规则:
public static $ rules = array(
'email'=>'required',);
谢谢!
只需在用户表上定义唯一规则:
public static $ rules = array(
'email'=>'required | unique:users | email');
In my app there is a simple form with one field (email) that give the possibility to register to the newsletter.
If i entry a new email, all it works fine. If i entry an email that already exists in the database i get the error SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry... Because i had defined that field as unique in the database.
All i want to do is to redirect::back()->with('message', 'email already registered') But i do not know how can i do this? I can just put an if statement in the method controller? Or i have to define it in $rules in the model, adding another rule:
public static $rules = array(
'email' => 'required',);
Thank you!
Just define a unique rule on your users table:
public static $rules = array(
'email' => 'required|unique:users|email');
这篇关于Laravel 4:避免重复进入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!