In this tutorial explain of laravel unique validation example. This example will help you apply unique validation in laravel. You can learn about how to use validation in Laravel. This example will help you laravel form request validation unique.
Sometime you want to ensure that a field is unique in the database so that someone can’t add the same item many time. Sometime we need to add unique validation for email, username etc. If you check unique email and username then you have check database query manually and do it using condition it is complex. But Laravel provide “unique” rule that will help to easily add unique validation.
First, You can create your own request class using below command.
Create request class:
php artisan make:request StoreUserDataRequest
php artisan make:request UpdateUserDataRequest
Now you can use it in your controller as like below.
Controller file code
<?php
namespace App\Http\Controllers;
use App\Http\Requests\StoreUserDataRequest;
use App\Http\Requests\UpdateUserDataRequest;
use App\Models\User;
class UserController extends Controller
{
public function storeUserData(StoreUserDataRequest $request)
{
/* Do something */
}
public function updateUserData(UpdateUserDataRequest $request, User $user)
{
/* Do something */
}
}
Now you can see simple example one by one and you can use anyone that you require for unique validation.
How to use unique validation in Laravel? Examples
Example 1: Laravel unique validation
app/Http/Request/StoreUserDataRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreUserDataRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
'name' => 'required',
'username' => 'required',
'email' => 'required|email|unique:users'
];
}
}
Example 2: Unique validation with column name
app/Http/Request/StoreUserDataRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreUserDataRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
'name' => 'required',
'username' => 'required',
'email' => 'required|email|unique:users,email'
];
}
}
Example 3: Unique validation with rule
app/Http/Request/StoreUserDataRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class StoreUserDataRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
'name' => 'required',
'username' => 'required',
'email' => ['required', Rule::unique('users')]
];
}
}
Example 4: Unique validation with update
app/Http/Request/UpdateUserDataRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdateUserDataRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
'name' => 'required',
'username' => 'required',
'email' => 'required|email|unique:users,email,'. $this->user->id
];
}
}
Example 5: Unique validation with update rule
app/Http/Request/UpdateUserDataRequest.php
When we try to update user data it will again check that the email is required and that there is not a user with the same email.
Actually happens is that it checks against the users table find the model saved previously, which has the same email as the user we are trying to update, so it returns a validation error. To get around this we need to update validation rule to let it know that it need to check that the email is unique in the table, except against the model we have saved previously.
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class UpdateUserDataRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
'name' => 'required',
'username' => 'required',
'email' => ['required', Rule::unique('users')->ignore($this->user)]
];
}
}
You can use anyone field that you need.
I hope this tutorial help you.