Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forcing A Unique Rule To Ignore A Given ID #15518

Closed
iamayaz opened this issue Sep 20, 2016 · 10 comments
Closed

Forcing A Unique Rule To Ignore A Given ID #15518

iamayaz opened this issue Sep 20, 2016 · 10 comments

Comments

@iamayaz
Copy link

iamayaz commented Sep 20, 2016

  • Laravel Version: 5.2.
  • PHP Version: 5.4.6

Description:

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     | john@gmail.com |
...............................
| 2 | 102     | john@gmail.com |

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.

$this->validate($request, [
            'email' => 'unique:email_list,email,102,user_id ',
]);

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

Steps To Reproduce:

@themsaid
Copy link
Member

'email' => 'unique:email_list,email,'.$user->id ?

@iamayaz
Copy link
Author

iamayaz commented Sep 20, 2016

@themsaid What value has $user->is ?

@themsaid
Copy link
Member

Replace $user->id with the primary key you want to ignore, here it's the user_id.

I'm closing since this is not a bug report, however feel free to ask on the forums or ping me if you need further help.

@iamayaz
Copy link
Author

iamayaz commented Sep 20, 2016

@themsaid i'm not validating with primary id as you can see in the table

@themsaid
Copy link
Member

You example was correct then:

'email' => 'unique:email_list,email,102,user_id ',

@iamayaz
Copy link
Author

iamayaz commented Sep 20, 2016

@themsaid yes that's why i'm asking here, because when i'm trying to add same email (john@gmail.com) with user_id = 105 then its return an error

'The email has already been taken.'

However it should not return error because i'm adding with different user_id. And when i'm passing same id like

'email' => 'unique:email_list,email,102,u_id',

Then it is return below error

QueryException in Connection.php line 729:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'asdas.ali@asd.com' for key 'email_list_email_unique' (SQL: insert into `email_list` (`email`, `u_id`, `updated_at`, `created_at`) values (asdas.ali@asd.com, 102, 2016-09-20 13:05:46, 2016-09-20 13:05:46))

@themsaid
Copy link
Member

You're ignoring 102 but there's another record with ID 101

@iamayaz
Copy link
Author

iamayaz commented Sep 20, 2016

@themsaid Aaahhhh... it's means i have to mentioned all exist id's as well ?

@themsaid
Copy link
Member

I don't think you can ignore multiple IDs, only 1.

I guess you'll need to create your own validation rule.

@iamayaz
Copy link
Author

iamayaz commented Sep 20, 2016

@themsaid Thank you so much, i'm going to try. If you have time then kindly Save My Time, I would like to appreciate. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants