As you know laravel is a popular framework based on the PHP language. It is a Model, View, Controller (MVC) structure and has many in-built features. So today in this tutorial we will learn the unique validation feature in laravel and how you can implement it in laravel 6, laravel 7, and laravel 8.
Suppose you have a website or application and you want to register users through a registration form with a unique email and username. And don’t want to mess with all other user’s details.
So in this case, we need to implement a unique validation feature on the laravel registration form. And set the unique validation rule for email and username or any field that you want. The validation rule through the errors if the same email and username already exist in the database.
So let’s get started!
There are two ways to add unique validation on email, username, or any other fields. First, we will with the database migration and second with form request. So let’s jump to define the validation while migration.
Unique Validation with Database Migration
To add the unique validation in laravel migration, you have to tell the laravel that field will be unique. And you can do this by the unique() method.
This case prompt, when you know that field will be unique in the database and no user will have the same data twice. See the example code.
In the above code, we applied a unique validation with the unique() method and made a username and email unique. It will create an index for username and email and prevent the same two rows of data insertion in the database. This will absolutely work fine.
But in that case, if you want to handle the error exception and present the more user-friendly then you need to go through form request validation. So let’s see the form request validation.
Laravel Form Request Unique Validation
In this way, you need to create a form request class and handle all validation in this class. You can also validate the form directly in the controller and save the data in the database but I would recommend to you create a separate request class and use it.
Let’s start with create a form request.
Create Request Class
We will use the php artisan command to generate the request class. See the following syntax command.
Inject Request File in Controller
Now, inject the request class into the controller that you just created.
Simple Unique Validation
Now we will unique validation rule to our StoreUserRequest class. Suppose you want to make a username unique and required field so see the simple validation rule example.
The above request validation rule will check the same data with the existing database. This means if you will try to register a user with the same email it will through the error.
Unique Validation with Column Name
You can also do the above same thing with defining a column name with the unique rule. The syntax is unique:table, column_name
Unique Validation with Update
When you try to edit the user and of course you want to check again that the user does not exist with the same email id by excluding the current user.
So here, you need to update the validation rule and pass the user ID with a unique rule. Then the validation rule ignores that ID to check against. See the code below.
Unique Validation with Rule Class
You can also apply a validation rule on the Model or table name column with the help of the Rule class. Just inject the Rule class into your form request class and use it. See the following example.
Unique Validation with Update Rule Class
All Available Validation Rules
There are many more validation rules in laravel you can check the following list of all available validation rules in laravel.
|Accepted If||Exclude If||Password|
|Active URL||Exclude Unless||Present|
|After (Date)||Exists (Database)||Prohibited|
|After Or Equal (Date)||File||Prohibited If|
|Alpha Dash||Greater Than||Regular Expression|
|Alpha Numeric||Greater Than Or Equal||Required|
|Array||Image (File)||Required If|
|Before (Date)||In Array||Required With|
|Before Or Equal (Date)||Integer||Required With All|
|Between||IP Address||Required Without|
|Boolean||JSON||Required Without All|
|Current Password||Less Than Or Equal||Size|
|Date Equals||MIME Types||Starts With|
|Date Format||MIME Type By File||String|
|Digits Between||Multiple Of||URL|
|Dimensions (Image Files)||Not In||UUID|
These are the available validation rules. You check on the official website here, all rules and their functions.
I hope this tutorial is helpful for you. If you have a query please let me know in the comment section, I’ll respond to you as soon as possible.