Soft Delete & Force Delete Example in Laravel

avatar
Feb 05, 2020 · Article · 3 min, 674 words

In this tutorial, we’re going to learn how soft delete works in Laravel. When models are soft deleted, they are not actually removed from your database. Instead, a timestamp is set on the deleted_at column.

Table of Contents

  1. Create Migration & Model
  2. Insert New Data
  3. Soft Delete
  4. Restore Deleted Data
  5. Force Delete (Permanently)

Create Migration & Model

We’ll test soft delete for products table. Let’s create a migration and model for products table:

php artisan make:model Product -m

Go to database/migrations folder and open timestamp_create_products_table.php file. We need to define $table->softDeletes(); line like this:

timestamp_create_products_table.php
<?php

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

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->float('price');
            $table->softDeletes();
            $table->timestamps();
        });
    }

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

From the app folder, open Product.php file. In the Product model, we have to include use SoftDeletes; line.

Product.php
<?php

namespace App;

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

class Product extends Model
{
    use SoftDeletes;

    public $fillable = ['title', 'price'];
}

Insert New Data

For testing purposes, I’ll test insert, delete tasks using Laravel Tinker. Let’s open Laravel Tinker by this command:

php artisan tinker

I’m inserting a new product:

$product = new Product;
$product->title = 'Test Product';
$product->price = 4.32;
$product->save();

We’ve inserted a new product. We can see all inserted products:

$products = Product::get();

Soft Delete

Normal delete will be the soft delete. Let’s delete the product softly:

Product::find($id)->delete();
// or,
Product::destroy($id);

If we soft delete any product, the delete_at column will be updated. To see the deleted data, we can write code like:

$products = Product::onlyTrashed()->get();

If we need to get both deleted and fresh data, there is a way to get both:

$products = Product::withTrashed()->get();

Restore Deleted Data

We can easily restore any deleted data like this:

Product::withTrashed()->find($id)->restore();

Force Delete (Permanently)

If we want to delete permanently, we have this option:

Product::find($id)->forceDelete();

To delete from soft-deleted (trashed) data, we need to write code like:

Product::onlyTrashed()->find(2)->forceDelete();

We can also set conditions at the time of deleting data from the trash. Let’s deleted 30 days of older data from soft deleted data.

Product::onlyTrashed()->where('deleted_at', '<', Carbon::subDays(30))->forceDelete();

The tutorial is over. Thank you.

Comments

No comments yet…