How to prevent insert email if already exist with specific id in laravel?
Asked Answered
I

2

4

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.

Intestine answered 20/9, 2016 at 10:21 Comment(5)
Do you want it to come back as valid if the email already exists with the same user_id? Similarly, do you want it to come back as valid if the email already exists with a different user_id?Lacedaemonian
'email' => 'unique:email_list,email,'.$user->id.',user_id' - documented under rule-uniqueBilliot
@Lacedaemonian your i want your first conditionIntestine
@BenSwinburne Thanks for guideline, let me check itIntestine
Please rephrase your question. It's not clear what you are trying to achieve.Ultrastructure
B
3

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'
Billiot answered 20/9, 2016 at 10:52 Comment(5)
In third parameter i will have to user id right? like 102 ?Intestine
Its returning an error QueryException in Connection.php line 729: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '[email protected]' for key 'email_list_email_unique' (SQL: insert into email_list (email, u_id, updated_at, created_at) values ([email protected], 102, 2016-09-20 10:59:11, 2016-09-20 10:59:11)Intestine
If you ignore the ID, it'll always validate and insert. What you meant when you replied to @Lacedaemonian you meant the second condition. In which case, 5.2 doesn't support it out of the box. What you want is validate the form if the email address is there, but doesn't belong to this user. If it exists, and belongs to the user, the form should not validate. You need to extend the validator- I'll update my answer for you.Billiot
I want to prevent to insert if same email address exist with same user id, like in table which i add in question the first and second record should not add again. But if user id is 103 then it should be add in table like | 3 | 103| [email protected]. Thanks for having attention.Intestine
I added some text in the question can you kindly view it. Appreciated...Intestine
D
0

It is against of exist rule.
First of all, the exist rule check based on the condition the fields is avail or not.
I think, if we used it as opposite then it is useful for you.

Ex:-

     'email' => 'exists:email_list,email,user_id,!102'

i.e.,

     'email' => 'exists:email_list,email,user_id,!'. $user_id        

otherwise,

     'email' => 'exists:email_list,email,user_id,!'. Input::get(user_id)

while using Input & import the

    use Illuminate\Support\Facades\Input;        

and change message content
Please try these and reply what happened.. or is it work for you..?

Disembroil answered 21/9, 2016 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.