Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Multiple Ways to Apply Unique Validation with Soft Delete in Laravel

Multiple Ways to Apply Unique Validation with Soft Delete in Laravel

Aman Jain, June 17, 2022June 17, 2022

In this article we will solve the problem of Unique Validation with Soft Delete in Laravel. Soft delete trait in laravel by default add the delete_at null query to all queries but in validation rules of unique it doesn’t apply because validation rule does not use Laravel eloquent model and Soft delete only apply to Eloquent models So to resolve this issue we need to manually add deleted_at column in unique validation.

This example will work in laravel 5, laravel 6, laravel 7 , laravel 8 and laravel 9 as well. Sometime we want to apply soft delete with unique on email, username, phone number, name etc.

There is multiple ways to apply the unique validation in laravel like we can create our own custom unique validation rule, using the inbuilt unique validation and also using the Rule class of validation.

So in this article we will cover all the ways to solve this problem. You can quickly solve as follow

'email' => 'required|email|unique:users,email,NULL,id,deleted_at,NULL'

Here unique is the rule name, users is table name, email is column name, NULL is value of id field which we want to exclude(useful at the time of update record), id name of column to exclude the record then we have deleted_at key which is used here to only check with NULL values.

If you want to apply it at the time of updating the data

'email' => 'required|email|unique:users,email,2,id,deleted_at,NULL'

Here 2 is id of row which is updating.

Let’s understand this with multiple example

Method 1 : Unique validation soft delete without Rule

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ArticleController extends Controller
{

 function validateUnique($request Request){
   
   $validator = Validator::make($request->all(), [
            'email' => 'required|email|unique:users,email,NULL,id,deleted_at,NULL'
        ]); 
}
}

In this method we have created a controller and a method validateUnique to validate the email along with soft deleted column deleted_at.

Method 2 : Unique validation soft delete without Rule on Updated

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ArticleController extends Controller
{

 function validateUnique($request Request){
   $authUser = User::find(1);
   $validator = Validator::make($request->all(), [
            'email' => 'required|email|unique:users,email,'.$authUser->id.',id,deleted_at,NULL'
        ]); 
}
}

In this method we have created a controller and a method validateUnique to validate the email along with soft deleted column deleted_at and excluded the auth user from the inclusion.

Method 3 : Unique validation soft delete with Rule

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Contracts\Validation\Rule;


class ArticleController extends Controller
{

 function validateUnique($request Request){
    
   $validator = Validato'r::make($request->all(), [
            'email' => ["required","email",
                       Rule::unique('users')->whereNull('deleted_at')]
        ]); 
}
}

In this method we have created a controller and a method validateUnique to validate the email along with soft deleted using Rule class column deleted_at.

Also Read : Unique validation in Laravel 8 with example

Method 4 : Unique validation soft delete with Rule on Update

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Contracts\Validation\Rule;


class ArticleController extends Controller
{

 function validateUnique($request Request){
   $authUser = User::find(1);
   $validator = Validator::make($request->all(), [
            'email' => ["required","email",
                       Rule::unique('users')->ignore($authUser->id)->whereNull('deleted_at')]
        ]); 
}
}

In this method we have created a controller and a method validateUnique to validate the email along with soft deleted using Rule class column deleted_at and excluded the auth user from the inclusion.

Also Read : How to make custom validation in Laravel 8 ?

Related

Php Laravel Laravel 9 laravelsoftdeleteunique

Post navigation

Previous post
Next post

Related Posts

Laravel How to get laravel application root path

How to get laravel application root path ?

October 13, 2022March 16, 2024

In this blog post, we’ve shown you how to get laravel application root path directory. If you need to get the path from anywhere in your application, you can use the Laravel public_path() helper function. If you need to get the path to your application’s storage directory, you can use…

Read More
Php How to Delete Folder Recursively With Files in Laravel ?

How to Delete Folder Recursively With Files in Laravel ?

May 25, 2022May 26, 2022

File System are used to store the information and how to manage the structure, so to perform the file system based operations like Delete Folder Recursively With Files it uses Illuminate\Filesystem\Filesystem class in laravel. Laravel provides inbuilt library to access the file system and we can do multiple robust operations…

Read More
Laravel Get Specific Columns Using with() function in laravel

How to get specific columns using with function in laravel ?

November 8, 2023March 16, 2024

When working with Laravel, a popular PHP framework, you’ll often need to retrieve specific columns using with() function in laravel from your database tables. Laravel provides a powerful and efficient way to do this using the with() function. This function allows you to specify which related models and their columns you want…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version