Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

soft delete in Laravel

How to use soft delete in Laravel 8 ?

In this tutorial, I will give you a simple example of laravel soft delete, So you can easily apply it with your laravel 5, laravel 6, laravel 7, and laravel 8 application, Sometimes we need to recover old data from our database, So we can retrieve record from database using sofdelete if we remove the wrong row.

Concept of Soft Delete

Actually, Soft Delete is a Complete Concept of Recycle bin which is Introduced in Laravel 5.

Using Softdelete you can create a complete Recycle Bin Concept, you can display deleted data on Trash and you can recover it from the database in your application.

Laravel provides support for soft deleting using the Illuminate\Database\Eloquent\SoftDeletes trait.

Complete Documentation : Working with Soft Delete..

How it Works

It applies to the deleted_at column on the table that is default will be null and when we remove then it will place the current timestamp, Laravel Model always fetch that record have only deleted_at = null.

When we add Illuminate\Database\Eloquent\SoftDeletes trait to the models then the softdelete works.

We all know softdelete is a basic requirement of each and every laravel application, So lets start with a example of softdelete in laravel with scratch.

Generating Migration :

php artisan make:migration create_softdelete_examples_table


Migration Structures :

<?php

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

class CreateSoftdeleteExamplesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('softdelete_examples', function (Blueprint $table) {
            $table->id();
            $table->string('name',25)->nullable();
            $table->string('subject',25)->nullable();
            $table->string('email',25);
            $table->longText('description');
            $table->softDeletes();
            $table->index(['deleted_at']);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('softdelete_examples');
    }
}

Add this column into your Database Migration File.

$table->softDeletes();


Run Migration :

php artisan:migrate
soft delete in Laravel 8


Make Model :

php artisan make:model Model\SoftdeleteExample

App\Model\SoftdeleteExample

<?php

namespace App\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

class SoftdeleteExample extends Model
{
    use SoftDeletes;

    protected $table = 'softdelete_examples';
    protected $dates = ['created_at', 'updated_at', 'deleted_at'];

}

Add Illuminate\Database\Eloquent\SoftDeletes trait to the models

use SoftDeletes;
soft delete in Laravel 8

It’s important to note that even though you added the SoftDeletes column to your model, Laravel doesn’t automatically use it until we add the soft delete trait so we will still irreparably delete data without it.

Let’s Delete Some Data

We have a ContactController where we write delete functionality.


Route :

Route::get('contact-delete/{id}','ContactController@contactDelete')->name('contact.delete');


Controller :

public function contactDelete(Request $request,$id){
        $delete = SoftdeleteExample::where('id', $id)->delete();
        Alert::success('Contact Data deleted Successfully!');
        return redirect()->back();
    }
soft delete in Laravel 8

Now, you can see when we delete a Row data, the deleted_at column is filled by timestamp value.


Retrieve Deleted Data from the table :

$deletedData = SoftdeleteExample::whereNotNull('deleted_at')->get();

In this article, we learned “How to use soft delete in Laravel 8”, I hope this article will help you with your Laravel application Project.


Also Read : Pagination in Laravel 8.