Laravel Unique Validation on Update
Tags: laravel, validation, laravel8, laravel9
Published: 2022-08-01 03:24:45

In this article, we will see laravel unique validation on update. unique validation rule in laravel is used when you want to validate any email id or mobile number. Here, we will see laravel unique email validation on update form or laravel unique validation with condition, laravel provide "unique" rule for email unique validation in laravel.

So, let's see how to add unique validation on update in laravel, laravel unique validation on update, unique validation rule in laravel 7/8, laravel unique email validation on update, laravel unique validation on insert, how to use unique validation in laravel.

Here, you can create request class for email unique validation in laravel, run below command to create request class.

```

php artisan make:request StoreUserRequest

php artisan make:request UpdateUserRequest

```

Your controller file look like the below code.

```
<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreUserRequest;

use App\Http\Requests\UpdateUserRequest;

use App\User;

  
class UserController extends Controller

{
    public function store(StoreUserRequest $request)

    {
        //Your Code
    }

    public function update(UpdateUserRequest $request, User $user)
    {
        //Your Code
    }
}
```

---
 
**_[Read Also: How To Get Current Date And Time In Node.js](https://techsolutionstuff.com/post/how-to-get-current-date-and-time-in-node-js)_**

---
 
> Example 1: Unique Validation with Column Name

Add the below code in this file app/Http/Requests/StoreUserRequest.php

```
<?php
  
namespace App\Http\Requests;
  
use Illuminate\Foundation\Http\FormRequest;
  
class StoreUserRequest extends FormRequest
{

    public function authorize()
    {
        return true;
    }
 
    public function rules()
    {
        return [
                'name' => 'required',
                'username' => 'required|min:8',
                'email' => 'required|email|unique:users,email'
            ];
    }
}
```
 
> Example 2: Simple Unique Validation

Add the below code in this file app/Http/Requests/StoreUserRequest.php

```
<?php
  
namespace App\Http\Requests;
  
use Illuminate\Foundation\Http\FormRequest;
  
class StoreUserRequest extends FormRequest
{

    public function authorize()
    {
        return true;
    } 
  
    public function rules()
    {
        return [
                'name' => 'required',
                'username' => 'required|min:8',
                'email' => 'required|email|unique:users'
            ];
    }
}
```

---
 
**_[Read Also: How To Validate URL In PHP With Regex](https://techsolutionstuff.com/post/how-to-validate-url-in-php-with-regex)_**

---
 
> Example 3: Unique Validation with Rule

Add the below code in this file app/Http/Requests/StoreUserRequest.php 

```
<?php
  
namespace App\Http\Requests;
  
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
  
class StoreUserRequest extends FormRequest
{
 
    public function authorize()
    {
        return true;
    }
  
    public function rules()
    {
        return [
                'name' => 'required',
                'username' => 'required|min:8',
                'email' => ['required', Rule::unique('users')]
            ];
    }
}
```
 
> Example 4: Unique Validation with Update

Add the below code in this file app/Http/Requests/UpdateUserRequest.php

```
<?php
  
namespace App\Http\Requests;
  
use Illuminate\Foundation\Http\FormRequest;
  
class UpdateUserRequest extends FormRequest
{
    public function authorize()
    {
        return false;
    }
  
    public function rules()
    {
        return [
                'name' => 'required',
                'username' => 'required|min:8',
                'email' => 'required|email|unique:users,email,'.$this->user->id
            ];
    }
}
```

---
 
**_[Read Also: Laravel 8 Form Validation Example](https://techsolutionstuff.com/post/laravel-8-form-validation-example)_**

---
 
> Example 5: Unique Validation with Update Rule

Add the below code in this file app/Http/Requests/UpdateUserRequest.php

```
<?php
  
namespace App\Http\Requests;
  
use Illuminate\Foundation\Http\FormRequest;
  
class UpdateUserRequest extends FormRequest
{
    public function authorize()
    {
        return false;
    }

    public function rules()
    {
        return [
                'name' => 'required',
                'username' => 'required|min:8',
                'email' => ['required', Rule::unique('users')->ignore($this->user)]
            ];
    }
}
```