Download the PHP package krishnaraj/laravel-cascading-soft-deletes without Composer
On this page you can find all versions of the php package krishnaraj/laravel-cascading-soft-deletes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download krishnaraj/laravel-cascading-soft-deletes
More information about krishnaraj/laravel-cascading-soft-deletes
Files in krishnaraj/laravel-cascading-soft-deletes
Package laravel-cascading-soft-deletes
Short Description Automatically cascade both soft deletes and restores to nested relationships in Laravel.
License MIT
Informations about the package laravel-cascading-soft-deletes
๐ Laravel Cascading Soft Deletes
Automatically cascade soft-deletes and restores across your Eloquent relationships โ safely, precisely, and with full multi-parent support.
โจ Why this package?
Laravel's built-in SoftDeletes trait only soft-deletes the model you call delete() on. Child models in HasOne, HasMany, or BelongsToMany relationships are left untouched โ leading to orphaned records, broken UI states, and restore operations that silently bring back incomplete data.
Laravel Cascading Soft Deletes solves all of that:
| Problem | Solution |
|---|---|
| Children not deleted when parent is soft-deleted | โ Cascade soft-deletes through any relationship depth |
| Children not restored when parent is restored | โ Cascade restores โ tracked precisely at the DB level |
| Restoring a child that was already independently deleted | โ Smart tracking skips independently-deleted children |
| Multiple parents holding the same child deleted | โ Multi-parent guard prevents premature restore |
| Force-deleting a parent leaving orphaned children | โ Cascade force-deletes with tracking cleanup |
| Many-to-many pivot records left after parent deletion | โ
Auto-detach BelongsToMany on delete |
๐ Requirements
| Dependency | Version |
|---|---|
| PHP | ^8.2 |
| Laravel | 11.x or 12.x |
๐ Installation
1. Install via Composer
2. Run Migrations
The package includes a migration that creates the cascade_deletions tracking table. Run it with:
The migration is loaded automatically โ no need to publish it unless you want to customise the schema.
3. (Optional) Publish the Config File
This copies config/cascading-soft-deletes.php into your application for customisation.
4. (Optional) Publish the Migration
If you prefer to manage the migration yourself (e.g., to rename the table):
โ๏ธ Basic Setup
Step 1 โ Add SoftDeletes and CascadesSoftDeletes to your model
Step 2 โ That's it!
๐ Relationship Support
HasOne and HasMany
These are the primary cascadeable relationships. When the parent is deleted, all related children are soft-deleted. When the parent is restored, only children that were cascade-deleted (not independently deleted) are restored.
Nested (Deep) Relationships
Use dot-notation to cascade through multiple levels:
When User is deleted:
Useris soft-deletedPostrecords are soft-deletedCommentrecords under each post are soft-deleted
When User is restored, the full chain is restored in reverse.
BelongsToMany (Many-to-Many)
Pivot records are detached (removed from the pivot table) when a parent is soft-deleted. Since pivot data is permanently lost, re-attaching on restore is not supported.
To keep pivot records on delete, set
detach_belongs_to_many => falsein the config or setprotected $cascadeDetachBelongsToMany = falseon the model.
๐ Smart Restore Behaviour
The restore system uses a database tracking table (cascade_deletions) โ not timestamp comparisons โ to decide which children to restore. This makes it:
- Precise: A child deleted independently before the parent's cascade will never be restored by the parent's restore.
- Multi-parent safe: If two parents both cascade-deleted a shared child, that child will only be restored once both parents are restored.
- Stale-safe: If a child was force-deleted or independently restored after the cascade, stale tracking records are cleaned up automatically.
Example: Independent Deletion Not Restored
Example: Multi-Parent Guard
โ๏ธ Configuration Reference
After publishing, edit config/cascading-soft-deletes.php:
๐๏ธ Per-Model Configuration
Every config option can be overridden on a per-model basis using protected properties. Model-level settings always take precedence over the global config.
Using a Method Instead of a Property
For dynamic relationship lists, you may implement cascadeRelationships() as a method:
๐ข Nesting Limit
The default maximum nesting depth is 3 levels (e.g. user โ post โ comment โ reply). You can configure this globally or per-model.
If a relationship path exceeds the limit, a NestingLimitExceededException is thrown at delete time:
๐ฅ Force Delete Cascade
When a model is force-deleted, the cascade hard-deletes all children (using forceDelete() if available, otherwise delete()). All tracking records are purged:
๐๏ธ The CascadeDeletion Model
The cascade_deletions table is exposed as a first-class Eloquent model for inspection or debugging:
Each record contains:
| Column | Description |
|---|---|
parent_type |
Fully-qualified class name of the parent model |
parent_id |
Primary key of the parent |
child_type |
Fully-qualified class name of the child model |
child_id |
Primary key of the child |
created_at |
When the cascade-delete occurred |
๐ก๏ธ Error Handling
Throwing exceptions (default)
By default (throw_on_error = true), any exception during cascading is logged and re-thrown, allowing your application to handle it:
Suppressing exceptions
Set throw_on_error = false (globally or per-model) to silently log errors via Laravel's Log::error() without interrupting execution:
Transaction rollback
When use_transaction = true and rollback_on_error = true (both default), any error during cascading will roll back the entire delete operation โ keeping your database consistent:
๐งช Testing
The test suite covers:
- Basic cascade soft-delete and restore (HasOne, HasMany, nested)
- Tracking record creation and cleanup
- Independent deletion not restored
- Multi-parent cascade guard
- BelongsToMany detach and config flags
- Force delete cascade and tracking cleanup
- String/UUID primary keys
- Custom nesting limits
- Exception handling and transaction rollback
๐ฆ Package Structure
๐ License
MIT ยฉ Krishnaraj
All versions of laravel-cascading-soft-deletes with dependencies
illuminate/database Version ^11.0|^12.0
illuminate/support Version ^11.0|^12.0