如何在 Laravel 中检查现有电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30102777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can check for an existing email in Laravel?
提问by cyber8200
I'm trying to check if the email is already exist my database on my subscribestable.
我正在尝试检查电子邮件是否已经存在于我的订阅表中的数据库中。
Form
形式
{!! Form::open(array('url' => '/subscribe', 'class' => 'subscribe-form', 'role' =>'form')) !!}
<div class="form-group col-lg-7 col-md-8 col-sm-8 col-lg-offset-1">
<label class="sr-only" for="mce-EMAIL">Email address</label>
<input type="email" name="subscribe_email" class="form-control" id="mce-EMAIL" placeholder="Enter email" required>
<!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
<div style="position: absolute; left: -5000px;"><input type="text" name="b_168a366a98d3248fbc35c0b67_73d49e0d23" value=""></div>
</div>
<div class="form-group col-lg-3 col-md-4 col-sm-4"><input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="btn btn-primary btn-block"></div>
{!! Form::close() !!}
Controller
控制器
public function postSubscribe() {
// Validation
$validator = Validator::make( Input::only('subscribe_email'),
array(
'email' =>'unique:subscribes',
)
);
if ($validator->fails()) {
return Redirect::to('/#footer')
->with('subscribe_error','This email is already subscribed to us.')
->withErrors($validator)->withInput();
}else{
dd("HERE");
$subscribe = new Subscribe;
$subscribe->email = Input::get('subscribe_email');
$subscribe->save();
return Redirect::to('/thank-you');
}
}
Debuging Steps
调试步骤
I tried inputting email that I know already exist in my db.
I want to my validation to fail, and redirect me back to my /#footer
(homepage).
我尝试输入我知道已经存在于我的数据库中的电子邮件。我想让我的验证失败,并将我重定向回我的/#footer
(主页)。
I try printing dd("HERE");
if my vaildation not fail.
dd("HERE");
如果我的验证没有失败,我会尝试打印。
BUT I keep getting HERE
to print which mean my validation is not failing.
Why is that happening ? I'm completely blank out now.
Can someone please point out what I missed ?
I know I am very close.
但是我一直在HERE
打印,这意味着我的验证没有失败。为什么会这样?我现在完全空白。有人可以指出我错过了什么吗?我知道我很亲近。
Thanks.
谢谢。
回答by Robert
Your db field name is email
not subscribe_email
, your input param name however is. Laravel defaults to the fieldname given with the input, so in your case subscribe_email
您的 db 字段名称email
不是subscribe_email
,但是您的输入参数名称是。Laravel 默认使用输入给出的字段名,所以在你的情况下subscribe_email
Try this:
尝试这个:
array(
'subscribe_email' => 'required|email|unique:subscribes,email',
)
This uses the db fieldname email
, like you have, but the validation is on the field subscribe_email
.
这使用 db fieldname email
,就像您一样,但验证在 field 上subscribe_email
。
回答by Emeka Mbah
try specifying the column name of email in subscribes table
尝试在订阅表中指定电子邮件的列名
$rules = array(
'email' => 'email|unique:subscribes,email'
);
回答by Dachomo Lodam
//put this in your controller
if (User::where('email', '=', Input::get('email'))->exists()) {
return back()->withErrors([
'message' => 'Email is already taken'
]);
//then go up and include
use Illuminate\Support\Facades\Input;
回答by Patrick Stephan
It looks like the reason this is not working is because you are trying to match the subscribe_email
field against validation for a email
field, no match is being made. To fix you need to do this:
看起来这不起作用的原因是因为您试图将subscribe_email
字段与字段的验证进行email
匹配,但没有进行匹配。要修复你需要这样做:
$validator = Validator::make( Input::only('subscribe_email'),
array(
'subscribe_email' =>'email|unique:subscribes,email',
)
);