Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
561 views
in Technique[技术] by (71.8m points)

php - How to prevent insert email if already exist with specific id in laravel?

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

For example in email_list table

|ID | user_id | email          |
...............................
| 1 | 101     | [email protected] |
...............................
| 2 | 102     | [email protected] |

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

Currently i'm trying simple laravel validation like this.

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

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

Edited After 3 Hours

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Forcing A Unique Rule To Ignore A Given 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}"

So in your case, it'd be as follows

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...