Email unique validation in Laravel ignore id on update

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 = 'renishkhunt@tryvary.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 = 'renishkhunt@tryvary.com'

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 = 'renishkhunt@tryvary.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.

Related Posts

  1. WordPress Button Shortcode – Add button to WordPress editor
  2. WordPress shortcode – How to create shortcode in WordPress
  3. WordPress change URL in database using MySQL Query
  4. PHP remove HTML tags from string using strip_tags
  5. Enable shortcodes in widget WordPress
  6. PHP remove non-empty directory programmatically
  7. PHP File upload step by step with example
  8. Laravel 6 integrate AdminLte admin theme jeroennoten
  9. WooCommerce add product to cart with custom price
  10. Laravel Eloquent where null and where not null query

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Search Posts

Subscribe To Newsletter

Get Early Access To New Articles, Plugins, Discount Codes And Quickly Brief Updates about tips and tricks

Join 308 other subscribers