如果Laravel中已经存在具有特定ID的电子邮件,如何防止插入电子邮件? [英] How to prevent insert email if already exist with specific id in laravel?

查看:108
本文介绍了如果Laravel中已经存在具有特定ID的电子邮件,如何防止插入电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张要存储电子邮件地址的表.这些电子邮件地址将用user_id保存.

I have table in which i'm trying to store email addresses. These email addresses will be save with user_id.

例如在email_list表中

|ID | user_id | email          |
...............................
| 1 | 101     | john@gmail.com |
...............................
| 2 | 102     | john@gmail.com |

在上表中,您可以看到使用不同的user_id保存的相同电子邮件地址,这就是我要执行的操作.

In above table you can see same email addresses save with different user_id, that's it what i'm trying to do.

目前,我正在尝试像这样的简单laravel验证.

Currently i'm trying simple laravel validation like this.

'email' => 'required|unique:email_list|email',

因此,如果行具有相同的user_id,是否有任何方法可以检查已经存在的电子邮件地址?我正在使用laravel 5.2.如果有人指导我,我将不胜感激.谢谢

So is there any way to check already exist email addresses if row has same user_id? I'm using laravel 5.2. I would like to appreciate if someone guide me. Thank you

3小时后编辑

我还在github中添加了与 issue 相同的问题.有人说我必须创建自己的验证规则.

I also add same question in the github as issue. A person is saying that i have to create own validation rule.

推荐答案

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

您可以将要忽略的ID指定为可选的第三个参数.此外,如果您的表使用的主键列名称不是id,则可以将其指定为可选的第四个参数

You can specify an ID to be ignored as the optional third parameter. Furthermore, if your table uses a primary key column name other than id, you may specify it as the optional fourth parameter

'email' => "unique:{$table},{$field},{$user->id},{$idField}"

因此,您的情况如下

'email' => "unique:email_list,email,{$user->id},user_id'

这篇关于如果Laravel中已经存在具有特定ID的电子邮件,如何防止插入电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆