Laravel Unique Validation on Update

Websolutionstuff | Sep-03-2021 | Categories : Laravel

Today in this post 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

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 below.

<?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
    }
}

 

 

Example 1 : Unique Validation with Column Name

Add 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 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'
            ];
    }
}

 

Example 3: Unique Validation with Rule

Add 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 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
            ];
    }
}

 

Example 5: Unique Validation with Update Rule

Add 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)]
            ];
    }
}

 


 

You may also like :

Recommended Post
Featured Post
How To Create Custom Error Page In Laravel 9
How To Create Custom Error Pag...

In this article, we will see how to create a custom error page in laravel 9. Here we will create a custom 404 error...

Read More

Feb-09-2023

Convert JSON String to JSON Object Javascript
Convert JSON String to JSON Ob...

In this example we will see convert JSON string to JSON object in Javascript. You can use the javascript JSON.parse...

Read More

Jul-14-2021

What Is Digital Marketing?
What Is Digital Marketing?

Digital marketing is the component of marketing that utilizes internet and online bas...

Read More

Oct-19-2021

Mastering Reactive Forms Validation in Angular 15
Mastering Reactive Forms Valid...

Welcome to "Mastering Reactive Forms Validation in Angular 15: A Comprehensive Guide." In this guide, we will...

Read More

Jun-14-2023