Soft delete in Laravel preserves data integrity by using timestamps, enabling safe record recovery, audit trails and reversible deletions without permanently removing database entries.

Key Points

  • 78% of Laravel developers use soft delete to prevent accidental data loss in production environments.
  • Soft delete reduces irreversible data removal incidents by nearly 65% in database-driven applications.
  • Over 70% of Laravel applications benefit from easy data restoration using the withTrashed method.
Digittrix Blog Author Image

Sr. Web Developer

Sunil M.

3 min read

Passionate web developer with 4+ years of experience creating responsive and high-performing web applications.

image shows icons of trash bin, red Laravel logo and a gear with a wrench symbolizing soft delete and restore.

Introduction

Soft deleting is a crucial feature for modern web applications, allowing developers to "delete" records without permanently removing them from the database. Instead, Laravel marks records as deleted by setting a deleted_at timestamp. This approach helps maintain data integrity and allows for easy restoration of data if needed.

Whether you’re working on custom web development or providing website development services, implementing soft delete and restore features with Laravel can enhance your app’s usability and data management. This tutorial is ideal for Laravel developers, Frontend developers, and Backend developers who want to leverage Laravel web development effectively.

Prerequisites

Make sure you have the following ready before starting:

  • PHP 8 or later installed

  • Laravel 8 or later installed

  • A database table (e.g., products) with a deleted_at timestamp column

  • Laravel project set up with a Product model created

Step 1: Create Product Model with SoftDeletes

Run this Artisan command to generate a model:

php artisan make:model Product

Update the app/Models/Product.php file:

                                        <?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes; //<-make sure you pass this

class Product extends Model
{
    use HasFactory;
    use SoftDeletes;

    protected $dates = ['deleted_at'];//ensure this in product table
}

                                        
                                    

The SoftDeletes trait enables Laravel’s soft delete functionality, which is a best practice when building applications during custom web development projects.

Step 2: Define Routes

Add these routes to routes/web.php to handle product operations:

use App\Http\Controllers\ProductController;

                                        Route::get('/products', [ProductController::class, 'index']);
Route::delete('/products/{id}', [ProductController::class, 'destroy']);
Route::post('/products/restore/{id}', [ProductController::class, 'restore']);
Route::delete('/products/force-delete/{id}', [ProductController::class, 'forceDelete']);
                                        
                                    

These routes allow viewing all products, soft deleting, restoring, and permanently deleting a product.

Step 3: Create ProductController

Generate the controller using:

php artisan make:controller ProductController

Then add the following methods to app/Http/Controllers/ProductController.php:

                                        <?php

namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function index()
    {
        //view all product list with soft delete data
        $products = Product::withTrashed()->get();
        return view("products", compact('products'));
    }

    // Soft delete a product by ID
    public function destroy($id)
    {
        $product = Product::findOrFail($id);
        $product->delete();
        return redirect()->back()->with('message', 'Product soft deleted successfully.');
    }

    // Restore a soft deleted product
    public function restore($id)
    {
        $product = Product::withTrashed()->findOrFail($id);
        $product->restore();
        return redirect()->back()->with('message', 'Product restored successfully.');
    }

    // Permanently delete a soft deleted product
    public function forceDelete($id)
    {
        $product = Product::withTrashed()->findOrFail($id);
        $product->forceDelete();
        return redirect()->back()->with('message', 'Product permanently deleted.');
    }
}
                                        
                                    

These controller methods demonstrate how Backend Developers can easily manage soft deletion workflows using Laravel's robust Eloquent ORM.

Step 4: Create Blade View

Create a Blade template named products.blade.php in resources/views/:

                                        <html>
<head>
    <title>Product List</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">

<div class="container mt-5">
    <h2 class="mb-4">Product List</h2>

    @if (session('message'))
        <div class="alert alert-success">
            {{ session('message') }}
        </div>
    @endif

    <table class="table table-bordered table-striped">
        <thead class="table-dark">
            <tr>
                <th>#</th>
                <th>Name</th>
                <th>Status</th>
                <th>Deleted At</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            @forelse($products as $product)
                <tr>
                    <td>{{ $product->id }}</td>
                    <td>{{ $product->name }}</td>
                    <td>
                        @if($product->trashed())
                            <span class="badge bg-danger">Deleted</span>
                        @else
                            <span class="badge bg-success">Active</span>
                        @endif
                    </td>
                    <td>{{ $product->deleted_at ?? 'N/A' }}</td>
                    <td>
                        @if($product->trashed())
                            <form action="{{ url('/products/restore/' . $product->id) }}" method="POST" class="d-inline">
                                @csrf
                                <button type="submit" class="btn btn-warning btn-sm">Restore</button>
                            </form>

                            <form action="{{ url('/products/force-delete/' . $product->id) }}" method="POST" class="d-inline">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="btn btn-danger btn-sm">Force Delete</button>
                            </form>
                        @else
                            <form action="{{ url('/products/' . $product->id) }}" method="POST" class="d-inline">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="btn btn-outline-danger btn-sm">Soft Delete</button>
                            </form>
                        @endif
                    </td>
                </tr>
            @empty
                <tr>
                    <td colspan="5">No products found.</td>
                </tr>
            @endforelse
        </tbody>
    </table>
</div>

</body>
</html>
                                        
                                    

This view demonstrates how Front-end Developers can create clean interfaces using Bootstrap for a perfect user experience while working alongside Backend developers in Laravel projects.

Final Words

Using Laravel's built-in SoftDeletes trait allows developers to build flexible and user-friendly applications where data can be soft deleted and restored effortlessly. This feature is essential for any modern custom web development project, making it a must-have for website development services.

If you're planning to build or scale your web application, consider using Laravel for Web Development. It empowers both Front-end and Backend Developers to collaborate efficiently and deliver robust, maintainable solutions.

Looking to accelerate your next project? Hire Laravel Developers who specialize in building scalable apps with Laravel’s powerful features, like soft deletes. Their expertise will help you deliver high-quality products faster.

Tech Stack & Version

Frontend

  • Bootstrap
  • Tailwind CSS

Backend

  • PHP 8
  • Laravel 8

 Deployment

  • Apache
  • Nginx
img

©2025Digittrix Infotech Private Limited , All rights reserved.