Laravel Packages

Laravel Deleted Models Package

Published
Laravel Deleted Models Package image

The Laravel Deleted Models package by Spatie automatically copies deleted models to a separate table:

According to Freek's writeup about the package, you can think of this package as a "recycle bin for models. The package achieves this through a database table called deleted_models and adding a KeepsDeletedModels trait to your models that you want to save backups of deleted models:

use Illuminate\Database\Eloquent\Model;
use Spatie\DeletedModels\Models\Concerns\KeepsDeletedModels;

class BlogPost extends Model
{
    use KeepsDeletedModels;
}

This package can also attempt to restore deleted models using a few different methods:

// $blogPost will be restored and returned
$blogPost = BlogPost::restore(5);

// $blogPost will be returned, but it is not saved in the DB yet
$blogPost = Blogpost::makeRestored($id);

BlogPost::restoreQuietly(5);

You can also tap into the restore process with a Closure if you want to modify the model during the restoration process. This package has other configuration options and considerations, like pruning the deleted models table on a schedule and how often that table is pruned.

You might wonder why you would use this package vs. the built-in soft deletes feature in Laravel core. Freek Van der Herten's writeup does an excellent job of comparing the tradeoffs of both approaches.

In summary, here are some pros and cons of soft deletes:

  • โž• Soft deletes is very convenient
  • โž• No data copying needed when "deleting" a model
  • โž• Deleted records benefit from any future alterations to the table schema
  • โž• You must keep soft deletes in mind when querying the database (i.e., scope where deleted_at=null)
  • โž– Referential integrity when soft deleting model associations, this must be done manually
  • โž– Undelete can be complex when restoring a model (and related models)

And according to Freek, here are some pros and cons of using a separate deleted models table:

  • โž• No need to adjust queries to account for deleted_at=null
  • โž• The database can protect referential integrityโ€”deleting a record will also delete all associated records
  • โž• Easy to permanently delete old data
  • โž– Harder to restore deleted data as you must copy it back (vs. setting the deleted_at=null to restore a model with simple soft deletes)

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Sponsored

acquaintsoft logo
Acquaint Softtech

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

Visit Acquaint Softtech

The latest

View all →
Slow Job Alerts and Replica Commit IDs in Laravel 13.40 image

Slow Job Alerts and Replica Commit IDs in Laravel 13.40

Read article
Flock: A Free, Native Herd Alternative for Laravel image

Flock: A Free, Native Herd Alternative for Laravel

Read article
Live Props for Real-Time Updates in Inertia.js v3.11 image

Live Props for Real-Time Updates in Inertia.js v3.11

Read article
Cooldown Dependency Policy now in Composer 2.11 image

Cooldown Dependency Policy now in Composer 2.11

Read article
Email Sending and Testing for Laravel in One Account image

Email Sending and Testing for Laravel in One Account

Read article
Decide with Jev: Tie Every AI Check to the Exact Draft image

Decide with Jev: Tie Every AI Check to the Exact Draft

Read article