DEV Community

Cover image for How to use Soft Delete in Laravel 8
Snehal Rajeev Moon
Snehal Rajeev Moon

Posted on

How to use Soft Delete in Laravel 8

In this blog post we are going to see what is Soft Delete and why we need it in our laravel application.

What is Soft Delete and Why we use it?
Soft delete will not totally remove data from database instead it will prevent data from selecting or mark record for deletion.
For example if we accidentally deleted data from database we cannot retrieve or restore it easily and will cause issue if it has been used within our project. To prevent it laravel provide support for Illuminate\Database\Eloquent\SoftDeletes trait which enables the functionality of soft delete. It means we can add Soft delete in our Eloquent model.

Let us see how to use it.

First we need to decide for which model we need to set it and add use SoftDeletes in that model with this Illuminate\Database\Eloquent\SoftDeletes trait, this trait will automatically cast deleted_at attribute DateTime/Carbon instance for us.

For example we have the functionality where we delete user instead hard delete now we will use soft delete in User's model.

Use the below code to add it in your application

<?php

namespace App\Models;

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

class User extends Model
{
    use SoftDeletes;
}
Enter fullscreen mode Exit fullscreen mode

When we use it, it will set deleted_at attribute, as well as we can also add deleted_at field/column to our database, for that laravel schema builder has provided a helper method as given below.

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

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

Schema::table('flights', function (Blueprint $table) {
    $table->dropSoftDeletes();
});
Enter fullscreen mode Exit fullscreen mode

When we delete data it will add current date n time in deleted_at field. When we fetch list of users from, the soft deleted users will automatically be excluded from all query results.

If we want to see the soft deleted data we can fetch it with help of trashed() method provided by Soft delete.

   $user->trashed()
Enter fullscreen mode Exit fullscreen mode
  1. It also provide restore() method to restore soft delete data.
   $user->restore();
Enter fullscreen mode Exit fullscreen mode
  1. This method is also used in query to restore multiple/mass data.
 User::withTrashed()
        ->restore();
Enter fullscreen mode Exit fullscreen mode
  1. If we want to fetch the record with soft deleted data we can use it withTrashed()
User::withTrashed()->get();
Enter fullscreen mode Exit fullscreen mode
  1. When we want to permanently delete data we can force delete it by using
$user->forceDelete();
Enter fullscreen mode Exit fullscreen mode
  1. If we want to fetch only soft deleted data from Users we can retrive it with help of onlyTrashed() method.
User::onlyTrashed()->get();
Enter fullscreen mode Exit fullscreen mode

We have seen multiple methods provided by soft delete. It means we can fetch, restore data with help of them.

Thank you for reading. 🦄 🦄 🦁

Top comments (1)

Collapse
 
alihassanalidev profile image
Ail Hassan

thanks