Email unique validation in Laravel ignore id on update
Laravel is popular and fastest growing PHP Framework. Today, We are going to learn Email unique validation in Laravel. Also, We are going to see Laravel validation unique on update ignore the current record ID. Laravel provides the best validation library using that we can easily validate the incoming request.
Using Laravel unique email validation is preventing from a duplicate value on the database. Laravel validation unique is required when we don’t want a user to enter the duplicate value like an email address. Unique validation is helpful when you want to keep the unique value for a specific column. For example email, username, slug etc…
Email unique validation in Laravel
First of all, We are going to create email unique validation in Laravel during creating a new record. Let’s open the controller file. Then after, Find the store method and write the validation like below.
$request->validate([ 'email' => 'required|unique:user|max:20', ]);
Now, Change the validation as per your requirement. The email is the field name replace it with your field name. Then after, You should replace the table name in my case the table name is a user. So, I have written unique:user replaces it with your table name.
As per the above validation, Laravel create MySQL query like
select * from user where email = 'email@example.com'
By default, Laravel uses the field name as a column name in MySQL query as we set on the validation. In this case, Laravel use email as a column name. But what happens when the field name and column name are different. In that case, We should define the column name in the validation. Let’s assume, The column name is email_address not email. Then, The validation should be like this.
$request->validate([ 'email' => 'required|unique:user,email_address|max:20', ]);
Now, Laravel use email_address as a column name instead of email like.
select * from user where email_address = 'firstname.lastname@example.org'
Finally, We are ready with the Laravel validation unique on the store method.
Laravel validation unique on update ignore id
Now, We are going to set Laravel validation unique on update method. On the update method, We should exclude the current record’s ID. Let’s see, What should we change the Laravel validation unique on update method.
$request->validate([ 'email' => 'required|unique:user,email,'.$userID.'|max:20', ]);
As per, The above unique validation will ignore the current User ID. Let’s assume I edit the 7 number user. Then After, Laravel create MySQL query like
select * from user where email = 'email@example.com' and id <> 7
Laravel check the email address with all users email except the 7 number user.
Finally, We are ready with email unique validation in Laravel. I have explained the unique validation with MySQL query.
In conclusion, Today I will learn you laravel validation unique on update with ignoring current record id. I hope this article is helpful for you. If you have any question please write the comment. We are always ready to help you.