Add soft delete to every modl and migration in laravel by default

In enterprise-level applications sometimes we need to restore deleted data or need to take a backup of that data. With Larave soft delete, we can easily soft delete and restore deleted data and in addition to methods, we can automatically filter deleted data while querying.

In Laravel, the Soft Delete trait handles all the operations for this functionality. We just have to add the deleted_at field into migration and use the trait in the model file.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Test extends Model
{
    use HasFactory, SoftDeletes;
}

In the above model, we just used the SoftDeletes trait in class and added import for the SoftDeletes class. So now Laravel will handle all query functionality for soft delete.

Same as for the model, we need to add a table column to our test table. Please check below migration file to add the below column:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestsTable extends Migration
{
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
            $table->softDeletes();
        });
    }

    public function down()
    {
        Schema::dropIfExists('tests');
    }
}

Implementing soft delete into the Laravel application is quite easy but you need to repeat this code into every model. But we can add common functionality like soft delete into the model and migration using Stub.

Laravel stubs are helper modules that enable us to customize the default Artisan generated classes. We can customize the default so-called Stub files that are the foundation of what is generated. We can modify those stubs and perform common operations for those classes.

Open the terminal and enter the below command to get stub files:

php artisan stub:publish

It will new folder called /stubs. Among those files, we are interested in everything around Models and Migrations. Let’s modify the stub files as per our requirements.

Modify stubs/migration.create.stub as below:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class {{ class }} extends Migration
{
    public function up()
    {
        Schema::create('{{ table }}', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->softDeletes();
        });
    }

    public function down()
    {
        Schema::dropIfExists('{{ table }}');
    }
}

Add code to create the deleted_at column into this file and it will add those codes to all further migration files.

We will do the same thing with the Model stub file. Open stubs/model.stub file and modify as below:

<?php

namespace {{ namespace }};

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class {{ class }} extends Model
{
    use HasFactory, SoftDeletes;
}

After these changes, all models and migration created will contain soft delete functionality. To test run the below command.

php artisan make:model ModelName-m

Conclusion

In this article, we have created functionality to add soft delete globally for all models and migration by default.