Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

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
Jump24 - UK Laravel Agency

Laravel Developers that Click into Place. Never outsourced. Never offshored. Always exceptional.

Visit Jump24 - UK Laravel Agency
Tinkerwell logo

Tinkerwell

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

Tinkerwell
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
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
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
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
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
Drop in comments for Filament with Commentions image

Drop in comments for Filament with Commentions

Read article
Laravel Starter Kits Now Include Toast Notifications image

Laravel Starter Kits Now Include Toast Notifications

Read article
Ship AI with Laravel: Stop Your AI Agent from Guessing image

Ship AI with Laravel: Stop Your AI Agent from Guessing

Read article
Making Laravel MongoDB Operations Idempotent: Safe Retries for Financial Transactions image

Making Laravel MongoDB Operations Idempotent: Safe Retries for Financial Transactions

Read article
FormRequest Strict Mode and Queue Job Inspection in Laravel 13.4.0 image

FormRequest Strict Mode and Queue Job Inspection in Laravel 13.4.0

Read article
Pretty PHP Info: A Modern Replacement for `phpinfo()` image

Pretty PHP Info: A Modern Replacement for `phpinfo()`

Read article