Laravel 7 Soft Delete With Unique Validation
Laravel 02-Mar-2021

Laravel 7 Soft Delete With Unique Validation

In this laravel soft delete with unique validation example, you will learn how to use soft delete with unique validation in laravel.

Sometime, you need to create or update data into database table. And you want to use unique validation with soft delete in laravel.

So this tutorial will guide you on how to use soft delete unique validation with inserting and updating data into database tables in laravel.

Before taking a look at the examples, first of you need to know how to use soft delete.

When models are soft deleted, they are not actually removed data from your database table. Instead, a timestamp is set on the deleted_at column. If a model has a non-null deleted_at value, the model has been soft deleted.

use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model
{
    use SoftDeletes;
    public $timestamps = true;
    protected $fillable = [
        'name',
        'email',
        'created_by',
        'updated_by',
    ];
}

Laravel Unique Validation with Soft Delete

This tutorial provides you some examples of soft delete with unique validation in laravel apps.

Here, we will discuss two examples of soft delete with unique valiation on insert data into DB and as well as update DB table in laravel apps.

Now, let’s take a look at examples of soft delete with unique validation as follow:

Example 1: On inserting data

public function store(Request $request)
{
    $request->validate([
        'name'=>'required|unique:form_types,name,NULL,id,deleted_at,NULL',
    ]);
    // Write your code here
}

Example 2: On updating data

public function update(Request $request, $id)
{
    $request->validate([
        'name'=>'required|unique:form_types,name,'.$id.',id,deleted_at,NULL',
    ]);
    // Write Code here
}