How to Implement Soft Delete in Laravel

183
soft delete in laravel

In programming deleting a record has two concepts i.e. Hard Delete and Soft Delete. Hard Delete means you are completely removing the record from the table whereas Soft Delete means that you are flagging a record as deleted in a particular table, instead of actually being deleting the record.

Normally, deleting a record means we are completing deleting the record from the database. On contrary, soft delete can be a lifesaver where we accidentally delete the crucial data. For example, there is a list of orders of customers. If we implement soft delete and the orders are accidentally deleted, we can retrieve the deleted orders.

So, how can we implement soft delete in Laravel? Well, it’s a simple and straightforward process. First of all, we need to decide which table should implement soft delete. Here, we will implement soft delete in users table. As stated above, soft delete simply stores the deleted datetime stamps on the deleted_at column in the database.

users table doesn’t implement soft delete by default. First, we need to add a deleted_at column. Let’s create a migration file to add required column. Simply run the following command and call helper method softDelete().

php artisan make:migration add_soft_delete_to_users_table --table=users

This will create a new migration file. Simply, add the below code to the newly generated migration file.

<?php

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

.........

Schema::table('users', function (Blueprint $table) {
    $table->softDeletes();
});

.......

Schema::table('users', function (Blueprint $table) {
    $table->dropSoftDeletes();
});

Now, run the migration and a new deleted_at column will be added in users table.

php artisan migrate

Finally, we need to use a trait softDeletes(). Open up your model and import the trait and use it inside your model. So, our final model looks like below.

<?php

namespace App\Models;

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

class User extends Model
{
    use SoftDeletes;
}

Voila!!! that’s it. Soft delete is implemented. Now, whenever we delete a record from the users table, it will only update the deleted_at column without actually deleting the record from the database.

When we fetch the record, it will ignore all the deleted records. So, how can we fetch all the records along with deleted ones? It’s simple. We need to call withTrashed()helper function on model.


User::withTrashed()->get();

If we want to fetch only the deleted records, we can call the onlyTrashed() method.


User::onlyTrashed()->get();

If we want to restore the deleted records, we need to call restore method.


$user->restore();

All of the delete function calls will soft delete the records. But, if we want to permanently delete the record, we need to force delete the record as in the example below.


$user->forceDelete();

That is all about soft delete in Laravel. It’s simple. Isn’t it? Don’t forget to drop your comments below.

Read More Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.