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 ?