All Guides About Soft Delete Laravel 8 And 9 With Examples

  • 16-06-2022
  • 862
  • Laravel 9
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

Soft deleting allows us to easily view the data as well as it can restore your data, It will store your data as it is before. It will not delete that row data from your database table it will keep backup your your table.

It can restore data and can restore hung data for a minimum of time when by mistake or accidentally deleted data by clicking.

Laravel Eloquent model provides that SoftDeletes support us using Laravel provider support for soft deleting using the Illuminate\Database\Eloquent\SoftDeletes trait.

Even though you can still show that soft delete data in your front table or easy to can restore huge data from the server in minimal time by just running a single query.

So In this post, we will take a discussion of laravel soft deleting.

The first part of to taking soft delete process we need to tackle is setting the database table to have SoftDeletes column.

Let's start adding a column in a database table with the name of deleted_at it will store a timestamp with the format of (Y-m-d h:i:s) that will will taking on the delete request when you raise for delete record.

Migration Column Add

Create your database migration file and add $table->softDeletes(); at time end to migration.

It will create deleted_at column in your table.

In this Example I have taken categories migration, What I want that on delete I want to keep soft delete data in case future cases might use that deleted data.

database/migrations

<?php

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

class CreateCategoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->tinyInteger('status')->default(1)->comment('1 active,2 deactive');
            $table->text('description')->nullable();
            $table->softDeletes(); // SOFT DELETE COLUMN DELETED AT
            $table->timestamps();
        });
    }

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

After creating migration just run the migration, Add the migration table in your database.

Run Migration

Migrate your table to your database using the below command.

php artisan migrate

use SoftDeletes

After setting up SoftDeletes in your migration. We just little configuration set in the Category model.

Add use SoftDeletes; in your Category model class as it is given in the example. After it imports from Illuminate\Database\Eloquent\SoftDeletes;

app\Models\Category.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes; // IMPORT

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use SoftDeletes; // Add In Your Model

    protected $fillable = [
        'name',
        'status',
        'description'
    ];
}

How to Delete

We can delete as it is before deleted, Using delete() method or destroy() method, There no any different as we deleted before.

use App\Models\Category;

Category::destroy($id);

Restoring a Model

In case we accidentally delete the data record Laravel makes it easy to restore that record usign restore() method.

use App\Models\Category;

$category = Category::find($id);

$category->restore();

// OUTPUT
=> true

Deleting a Model

Let's say if you want to delete unusual data from the database which you don't want to restore, want to delete permanent, So not just make it delete, SoftDeletes also provides force delete foreceDelete() this function will delete data without keep in backup.

use App\Models\Category;

$category = Category::find($id);

$category->forceDelete();

// OUTPUT
=> true

Finding a Deleted Model

You just want to show soft deleted data too add withTranshed() method in your model query.

By default, it will not show you soft deleted data.

use App\Models\Category;

$category = Category::withTrashed()->find($id);

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :