unique

How to Use Unique Validation in Laravel

Hello Geek, I’d like to demonstrate how to use unique validation in Laravel 6, Laravel 7, Laravel 8, or Laravel 9 application. I’ll show you several methods for incorporating unique validation into your application. You can add unique validation at both the create and update times.
We sometimes need to add unique validation for email, username, and so on. If you want to check for a unique email or username, you must write a database query manually and use an if condition. However, Laravel provides a “unique” rule that will allow you to easily add unique validation.

Create Request Class:

Essentially, you can create your own request class by using the following command:

php artisan make:request StoreUserRequest

php artisan make:request UpdateUserRequest

Now you can use it, in your controller as like bellow:

Controller File 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)

    {

        /* Do Something */

    }

  

    public function update(UpdateUserRequest $request, User $user)

    {

        /* Do Something */

    }

}

Now you can look at the solutions below one by one and use whichever one you need for unique validation.

Example 1: Simple Unique Validation

app/Http/Requests/StoreUserRequest.php

<?php
  
namespace App\Http\Requests;
  
use Illuminate\Foundation\Http\FormRequest;
  
class StoreUserRequest 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
     */
    public function rules()
    {
        return [
                'name' => 'required',
                'username' => 'required|min:8',
                'email' => 'required|email|unique:users'
            ];
    }
}

Example 2: Unique Validation with Column Name

app/Http/Requests/StoreUserRequest.php

<?php
  
namespace App\Http\Requests;
  
use Illuminate\Foundation\Http\FormRequest;
  
class StoreUserRequest 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
     */
    public function rules()
    {
        return [
                'name' => 'required',
                'username' => 'required|min:8',
                'email' => 'required|email|unique:users,email'
            ];
    }
}

Example 3: Unique Validation with Rule

app/Http/Requests/StoreUserRequest.php

<?php
  
namespace App\Http\Requests;
  
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
  
class StoreUserRequest 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
     */
    public function rules()
    {
        return [
                'name' => 'required',
                'username' => 'required|min:8',
                'email' => ['required', Rule::unique('users')]
            ];
    }
}

Example 4: Unique Validation with Update

app/Http/Requests/UpdateUserRequest.php

<?php
  
namespace App\Http\Requests;
  
use Illuminate\Foundation\Http\FormRequest;
  
class UpdateUserRequest 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
     */
    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

app/Http/Requests/UpdateUserRequest.php

<?php
  
namespace App\Http\Requests;
  
use Illuminate\Foundation\Http\FormRequest;
  
class UpdateUserRequest 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
     */
    public function rules()
    {
        return [
                'name' => 'required',
                'username' => 'required|min:8',
                'email' => ['required', Rule::unique('users')->ignore($this->user)]
            ];
    }
}

Best wishes, fellow nerd.

Leave a Reply