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.

    '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 = ''

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.

    '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 = ''

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.

    '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 = '' 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. MySQL value BETWEEN Two Columns in Laravel
  2. PHP tree structure for categories and sub categories without looping a MySQL query
  3. WordPress Button Shortcode – Add button to WordPress editor
  4. WordPress shortcode – How to create shortcode in WordPress
  5. WordPress change URL in database using MySQL Query
  6. PHP remove HTML tags from string using strip_tags
  7. Enable shortcodes in widget WordPress
  8. PHP remove non-empty directory programmatically
  9. PHP File upload step by step with example
  10. Laravel 6 integrate AdminLte admin theme jeroennoten

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 357 other subscribers

%d bloggers like this: