See rates for the top Laravel developers in Latin America

Preserving Data Integrity with Laravel Soft Deletes for Recovery and Compliance

Last updated on by

Preserving Data Integrity with Laravel Soft Deletes for Recovery and Compliance image

Laravel's soft delete functionality provides a sophisticated approach to data management that maintains complete records while allowing logical deletion. This feature proves essential for compliance requirements, data recovery scenarios, and maintaining referential integrity across complex applications.

Setting Up Soft Deletes

Add the required database column using Laravel's migration helper:

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

Enable soft deletes in your Eloquent model:

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
 
class Document extends Model
{
use SoftDeletes;
 
protected $fillable = ['title', 'content', 'category', 'author_id'];
}

Basic operations with soft deletes work seamlessly:

// Soft delete a document
$document->delete();
 
// Restore a soft-deleted document
$document->restore();
 
// Check if a document is soft deleted
if ($document->trashed()) {
// Handle soft-deleted state
}

Consider a legal document management system where maintaining complete audit trails and ensuring regulatory compliance are paramount. Organizations need to track document lifecycle changes while preserving historical data for litigation and compliance purposes:

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
 
class LegalDocument extends Model
{
use SoftDeletes;
 
protected $fillable = [
'title', 'document_type', 'classification', 'retention_period',
'author_id', 'department_id', 'version', 'content_hash'
];
 
protected $casts = [
'retention_expires_at' => 'datetime',
'archived_at' => 'datetime',
];
 
public function author(): BelongsTo
{
return $this->belongsTo(User::class, 'author_id');
}
 
public function department(): BelongsTo
{
return $this->belongsTo(Department::class);
}
 
public function revisions(): HasMany
{
return $this->hasMany(DocumentRevision::class);
}
 
public function annotations(): HasMany
{
return $this->hasMany(DocumentAnnotation::class);
}
 
protected static function booted()
{
static::deleting(function ($document) {
// Cascade soft delete to related annotations
$document->annotations()->delete();
 
// Log deletion for audit trail
ActivityLog::create([
'action' => 'document_deleted',
'subject_type' => LegalDocument::class,
'subject_id' => $document->id,
'user_id' => auth()->id(),
'properties' => [
'document_title' => $document->title,
'classification' => $document->classification,
'deletion_reason' => request()->input('deletion_reason'),
],
]);
});
 
static::restoring(function ($document) {
// Restore related annotations
$document->annotations()->restore();
 
// Log restoration
ActivityLog::create([
'action' => 'document_restored',
'subject_type' => LegalDocument::class,
'subject_id' => $document->id,
'user_id' => auth()->id(),
'properties' => [
'document_title' => $document->title,
'restored_from' => $document->deleted_at,
],
]);
});
}
 
public function scopeAwaitingDestruction($query)
{
return $query->onlyTrashed()
->where('retention_expires_at', '<', now())
->where('deleted_at', '<', now()->subMonths(6));
}
 
public function isPermanentlyDeletable(): bool
{
return $this->trashed() &&
$this->retention_expires_at < now() &&
$this->deleted_at < now()->subMonths(6);
}
}
 
class DocumentRevision extends Model
{
use SoftDeletes;
 
protected $fillable = [
'document_id', 'version_number', 'change_summary',
'content_diff', 'author_id'
];
 
public function document(): BelongsTo
{
return $this->belongsTo(LegalDocument::class, 'document_id');
}
}
 
class DocumentAnnotation extends Model
{
use SoftDeletes;
 
protected $fillable = [
'document_id', 'author_id', 'annotation_text',
'page_number', 'position_data'
];
 
public function document(): BelongsTo
{
return $this->belongsTo(LegalDocument::class, 'document_id');
}
}

Advanced querying capabilities handle complex compliance scenarios:

<?php
 
namespace App\Services;
 
use App\Models\LegalDocument;
use Illuminate\Support\Collection;
 
class DocumentComplianceService
{
public function getActiveDocuments(): Collection
{
return LegalDocument::where('classification', '!=', 'archived')
->orderBy('updated_at', 'desc')
->get();
}
 
public function getDeletedDocumentsForAudit(): Collection
{
return LegalDocument::onlyTrashed()
->with(['author', 'department'])
->orderBy('deleted_at', 'desc')
->get();
}
 
public function getAllDocumentsIncludingDeleted(): Collection
{
return LegalDocument::withTrashed()
->with(['author', 'department', 'annotations'])
->orderBy('created_at', 'desc')
->get();
}
 
public function findDocumentForLitigation(int $documentId): ?LegalDocument
{
// Include soft-deleted documents for legal discovery
return LegalDocument::withTrashed()
->with(['revisions.author', 'annotations'])
->find($documentId);
}
 
public function getExpiredDocumentsAwaitingDestruction(): Collection
{
return LegalDocument::awaitingDestruction()
->with(['author', 'department'])
->get();
}
 
public function performRetentionCleanup(): array
{
$expiredDocuments = $this->getExpiredDocumentsAwaitingDestruction();
$deletedCount = 0;
$errors = [];
 
foreach ($expiredDocuments as $document) {
try {
if ($document->isPermanentlyDeletable()) {
// Force delete after retention period
$document->forceDelete();
$deletedCount++;
 
ActivityLog::create([
'action' => 'document_permanently_deleted',
'subject_type' => LegalDocument::class,
'subject_id' => $document->id,
'properties' => [
'document_title' => $document->title,
'retention_expired' => $document->retention_expires_at,
'soft_deleted_at' => $document->deleted_at,
],
]);
}
} catch (\Exception $e) {
$errors[] = [
'document_id' => $document->id,
'error' => $e->getMessage(),
];
}
}
 
return [
'deleted_count' => $deletedCount,
'errors' => $errors,
];
}
 
public function restoreDocumentWithRelated(int $documentId): bool
{
$document = LegalDocument::withTrashed()->find($documentId);
 
if (!$document || !$document->trashed()) {
return false;
}
 
// Restore document and all related annotations
$document->restore();
 
return true;
}
}
 
class DocumentController extends Controller
{
public function __construct(
private DocumentComplianceService $complianceService
) {}
 
public function destroy(LegalDocument $document, Request $request)
{
$request->validate([
'deletion_reason' => 'required|string|max:500',
]);
 
$document->delete();
 
return response()->json([
'message' => 'Document soft deleted successfully',
'can_restore' => true,
'deleted_at' => $document->fresh()->deleted_at,
]);
}
 
public function restore(int $documentId)
{
$restored = $this->complianceService->restoreDocumentWithRelated($documentId);
 
if (!$restored) {
return response()->json(['message' => 'Document not found or not deleted'], 404);
}
 
return response()->json(['message' => 'Document restored successfully']);
}
 
public function audit()
{
return response()->json([
'active_documents' => $this->complianceService->getActiveDocuments()->count(),
'deleted_documents' => $this->complianceService->getDeletedDocumentsForAudit()->count(),
'pending_destruction' => $this->complianceService->getExpiredDocumentsAwaitingDestruction()->count(),
]);
}
}

Soft deletes in Laravel provide a comprehensive solution for maintaining data integrity while supporting complex business requirements. This approach enables robust audit trails, facilitates compliance with data retention policies, and ensures recovery capabilities without compromising application performance.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Cut PHP Code Review Time & Bugs into Half with CodeRabbit logo

Cut PHP Code Review Time & Bugs into Half with CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.

Cut PHP Code Review Time & Bugs into Half with CodeRabbit
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
What's new in Filament v4.5? image

What's new in Filament v4.5?

Read article
Manage Waitlists in Laravel with Laravel Waitlist image

Manage Waitlists in Laravel with Laravel Waitlist

Read article
Laravel Introduces Official AI Documentation image

Laravel Introduces Official AI Documentation

Read article
Laravel Gets a Claude Code Simplifier Plugin image

Laravel Gets a Claude Code Simplifier Plugin

Read article
Laravel Boost Update Adds Support for the New MCP Protocol image

Laravel Boost Update Adds Support for the New MCP Protocol

Read article
Pest Adds withHost for Browser Testing Subdomains in Laravel image

Pest Adds withHost for Browser Testing Subdomains in Laravel

Read article