PHP

Manage Data Lifecycle with Laravel Eloquent Soft Deletes

Learn to implement soft deletes in your Laravel models, allowing you to 'recycle' records instead of permanently deleting them, with options to restore or force delete data.

<?php

namespace App\Models;

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

class Product extends Model
{
    use SoftDeletes;

    protected $fillable = ['name', 'price'];
}

// Migration to add 'deleted_at' column:
/*
Schema::table('products', function (Blueprint $table) {
    $table->softDeletes(); // Adds deleted_at timestamp column
});
*/

// Usage in a controller or elsewhere:

namespace App\Http\Controllers;

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

class ProductController extends Controller
{
    public function manageSoftDeletes()
    {
        // Create a product
        $product1 = Product::create(['name' => 'Laptop', 'price' => 1200]);
        echo "Created product: {$product1->name}
";

        // Soft delete the product
        $product1->delete(); // Sets 'deleted_at' timestamp
        echo "Soft deleted product: {$product1->name}
";

        // Find all products (excluding soft-deleted ones by default)
        $allActiveProducts = Product::all();
        echo "Active products count: " . $allActiveProducts->count() . "
";

        // Retrieve all products, including soft-deleted ones
        $allProductsWithTrashed = Product::withTrashed()->get();
        echo "All products (including trashed) count: " . $allProductsWithTrashed->count() . "
";

        // Retrieve only soft-deleted products
        $onlyTrashedProducts = Product::onlyTrashed()->get();
        echo "Only trashed products count: " . $onlyTrashedProducts->count() . "
";

        // Restore a soft-deleted product
        if ($product1->trashed()) {
            $product1->restore(); // Sets 'deleted_at' to null
            echo "Restored product: {$product1->name}
";
        }

        // Verify restoration
        $restoredProduct = Product::find($product1->id);
        echo "Product active after restore: " . ($restoredProduct ? 'Yes' : 'No') . "
";

        // Force delete (permanently delete) a product
        $product2 = Product::create(['name' => 'Mouse', 'price' => 25]);
        $product2->forceDelete(); // Permanently removes from database
        echo "Force deleted product: {$product2->name}
";

        return "Soft delete operations complete. Check console/logs.";
    }
}
How it works: Soft deletes in Laravel Eloquent provide a way to 'delete' records without actually removing them from your database. Instead, a `deleted_at` timestamp is set on the record. By using the `SoftDeletes` trait, Eloquent automatically excludes soft-deleted records from query results. You can retrieve soft-deleted records with `withTrashed()`, only soft-deleted records with `onlyTrashed()`, and restore them with `restore()`. For permanent deletion, `forceDelete()` bypasses the soft delete mechanism.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs