spatie/laravel-deleted-models
Automatically copy deleted Eloquent model attributes into a deleted_models table to create a recycle bin for records. Restore deleted models by ID when needed. An alternative to soft deletes for preserving deletion history without keeping rows in place.
SoftDeletes). Ideal for compliance, auditing, or temporary data retention needs.deleted_models), reducing clutter in the primary database.DeletedModel::where() or restoring via Model::restore(), enabling custom recovery logic.deleting) and model traits (HasDeletedModels), requiring minimal customization.deleted_models table, but requires manual adjustments for complex relationships or custom attributes.delete() adds latency (mitigated by batching or async processing if needed).deleting logic may conflict with package hooks.delete()/restore() operations?deleted_models table require inclusion in backups? If so, how to optimize storage?logging channel) for compliance?spatie/laravel-model-states (included as a sub-dependency) for state management.composer require spatie/laravel-deleted-models
php artisan vendor:publish --provider="Spatie\DeletedModels\DeletedModelsServiceProvider"
php artisan migrate
HasDeletedModels trait to target models:
use Spatie\DeletedModels\HasDeletedModels;
class BlogPost extends Model
{
use HasDeletedModels;
}
deleted_models table name or attributes via trait configuration.delete()/restore() workflows.SoftDeletes trait (mutually exclusive). Requires refactoring.delete() method if additional logic (e.g., notifications) is needed.deleting events (e.g., audit logs). Use middleware or event prioritization.Spatie\DeletedModels\PurgeDeletedModels).DeletedModel model events (deleted, restored).getDeletedModelAttributes() for custom attribute handling.deleted_models table growth and query performance.DB::enableQueryLog() to trace attribute copying delays.deleted_models.deleted_at and deleted_models.model_type for large datasets.delete() events) for high-throughput systems.deleted_models table size; set soft limits (e.g., purge records >30 days old).| Scenario | Impact | Mitigation |
|---|---|---|
| Database corruption | Lost deleted records | Regular backups; transaction rollback tests. |
Concurrent delete() |
Partial attribute copying | Database transactions or optimistic locking. |
| Storage exhaustion | Failed inserts/restores | Alerting on table size; auto-archiving. |
| Package version conflicts | Breaking changes | Test against all supported Laravel versions. |
| Custom logic conflicts | Silent failures | Pre-integration spike testing. |
delete() copies all required attributes.deleted_models table and revert trait usage if issues arise.How can I help you explore Laravel packages today?