LAKHDAR Mohamed
LAKHDAR Mohamed

Reputation: 11

The value of the email address field is already used. laravel request validation

I have a problem with my validation inside the request field, I made the email unique and it's workin' pretty good when i add new rows, but when I update them I have a problem and that's because of the user data base, so how can i ignore the email field in the update step?

Here is my validation line: 'email' => 'required | unique:users,email'

I'm actually working on adding employee and automatically adding user so i am trying now to add the validation into the employee request and at the same time find a solution that can makes the email be ignored while the updating progress is on

Thank you so much.

Upvotes: 1

Views: 252

Answers (3)

Adnan Siddique
Adnan Siddique

Reputation: 422

Can you share your update method from the controller to see your query. I think your update query is not taking email from the request but instead it is adding old one. In that condition too, if update query would work it shouldn't show unique error. So, it must be there that your query is not updating but instead it is adding new entry to the field. Remove unique from the validation and check if it is adding new entry or updating the entry. If it is adding new entry then you should retry your query. But it would be helpful if you can share your update method here.

Upvotes: 0

Abhishek
Abhishek

Reputation: 11

'email' => 'unique:users,email_address,'.$user->id

Upvotes: 1

Peppermintology
Peppermintology

Reputation: 10210

Update your validation rules to ignore the unique requirement if it is triggered for the current user:

'email' => 'required|email|unique:users,email,'.auth()->user()->id,

The above will prevent other users for changing their email to one that exists in the database, however, will not trigger a unique error when they are just updating their own information.

Upvotes: 1

Related Questions