Unique email restriction for Laravel 4 authentication

I am writing a Laravel 4 application with a users table with the usual contact information. In my users model, my email check indicates 'email'=>'required|email|unique:users', , which works great when registering new users.

My question is how to handle the user editing form - when the form is submitted, I would like the restriction on the unique email element to be fired only if it does not match the old email address - otherwise you cannot save your profile because the email address (your email address) is already in use.

thanks

+5
source share
2 answers

The third parameter of the unique rule allows you to specify the record identifier to ignore. When you edit a user, you want your unique validation rule to ignore the value contained in the identifier of the user you are editing.

 'email'=>'required|email|unique:users,email,'.$userId 

You can see the documents in the validation rule here .

The hardest part you'll come across is figuring out how to edit your rule to provide the identifier of the user you are editing. It all depends on how your rules are set and where you perform your check.

+6
source

Unique validation syntax

unique:table,column,except,idColumn

Try this to exclude your current email address.

unique:'users', 'email', Auth::user()->email

+1
source

Source: https://habr.com/ru/post/1211902/


All Articles