Laravel Validation - Unique rule with multiple column and condition.

Today I am going to share, How we can use Laravel validation rule `unique`.

If you have worked with laravel and familiar with laravel validation, you must know what does 'unique' rule checks.

If not let me give here a brief about this. In simple words, It is similar to a unique key constraint applied to a column in the database table.

'Unique' rule check there is no similar value exists in the given database table column, to value matching with the provided input value.

The general format of the rule is:

unique:table //when input name is similar to column name
OR
unique:table,column
OR
unique:Model,attribute

Examples:

'email' => 'unique:users' //here users is table name
OR
'email' => 'unique:c,email_address' //can give custom column name.
OR
'email' => 'unique:User,email'  //here User is a model class

This is a very simple usage of the Unique validation rule.

Updating the record

One of the common situation or problem faced by developers while using the unique validation rule occurs while updating a record.
Let's understand why it happens:

As per rule statement it checks, there shouldn't exist any similar value to the inserting value in the column.
But in-case of updating a record, consider the situation when you are updating the record without any change in the unique column value. In this case 'Unique' check there is already a value similar to the inserting value in the column for same record (which should be fine) and raise a validation fail.
In this case, we need to inform the rule to skip that particular record while checking for duplicate records.

How we can do that?
By, passing the Id column(primary key) - value combination in reverse order.i.e. value,column.
See below syntax:

unique:table,column,except,idColumn

Examples:

'email' => 'unique:users,email,'.$input['id']. ',id' //provide dynamic value of Id column

In case creating a new record, the value id will be NULL.

'email' => 'unique:users,email,NULL,id'
which is similar to:
'email' => 'unique:users'

Unique on Multiple Column

Sometimes the situation comes:
When you have to apply unique on multiple columns.
When you want to ignore all soft-deleted records while validating for unique values

In this kind of situation you just need to append the column, value combination to the rule. Like:

'email' => 'unique:users,email,' .$input['id']. ',id,deleted_at,NULL'

Comments

  1. Nice articel, This article help me very well. Thank you. Also please check my article on my site Know All About Htaccess Tutorial. In link article we will learn about How to use .htaccess file?.

    ReplyDelete
  2. This is a good post. Laravel soft delete. Our Readerstacks blogs are the most useful for software, PHP, Angular, and Laravel to improve their skills? Read our updated list to find out and become a better coder.

    ReplyDelete

Post a Comment

Popular posts from this blog

Using Virtual Columns in Laravel - Accessors and Appends

How to Show Cookie Policy Consent or GDPR Popup in Laravel using Cookie.

Postman Collection Run - How to Test File Uploading API on CircleCi or Jenkins