Download the PHP package workdoneright/laravel-deletion-guard without Composer
On this page you can find all versions of the php package workdoneright/laravel-deletion-guard. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download workdoneright/laravel-deletion-guard
More information about workdoneright/laravel-deletion-guard
Files in workdoneright/laravel-deletion-guard
Package laravel-deletion-guard
Short Description Config-driven deletion guard for Laravel models
License MIT
Homepage https://github.com/1639302-abishekrsrikaanth/laravel-deletion-guard
Informations about the package laravel-deletion-guard
Laravel Deletion Guard
A Laravel package that prevents Eloquent models from being deleted when they have dependent relationships with existing data. Stop accidental data loss by automatically blocking deletions when related records exist.
Features
- ๐ก๏ธ Automatic Protection - Prevents deletion of models with dependent relationships
- ๐ฏ Two Modes - Choose between explicit method-based or docblock annotation-based configuration
- ๐ Performance Optimized - Built-in caching for relation discovery
- ๐ง Flexible - Support for force delete, soft deletes, and custom error messages
- ๐ Record Counts - Automatically include counts in error messages (e.g., "47 posts, 123 comments")
- ๐๏ธ Cascade Delete - Optionally auto-delete related records instead of blocking
- ๐๏ธ Conditional Blocking - Block deletion only when specific conditions are met
- ๐ Laravel Gates Compatible - Includes a policy class for authorization
- ๐งช Audit Command - Test deletion blockers before attempting deletion
Installation
Install the package via composer:
Publish the config file:
Configuration
After publishing, the config file will be available at config/deletion-guard.php:
Usage
Choosing a Mode
The package supports two configuration modes:
Explicit Mode (Recommended)
- โ Full feature support including conditional blocking
- โ Type-safe with IDE autocomplete
- โ More control and flexibility
- โ Best for complex business logic
Docblock Mode
- โ Clean, annotation-driven syntax
- โ Less code, more readable
- โ Great for simple blocking rules
- โ No conditional blocking (security limitation)
See the Mode Comparison table below for detailed feature parity.
Basic Setup
Add the PreventsDeletionWithDependencies trait to your Eloquent models:
Explicit Mode (Recommended)
Define which relationships should block deletion by implementing the deletionDependencies() method:
Now when you try to delete a user with posts or comments:
Docblock Mode
Set mode to 'docblock' in your config, then use annotations:
Docblock Mode Advanced Features
Docblock mode supports most features available in explicit mode:
Available Docblock Annotations:
@deleteBlocker- Marks relation as a deletion blocker (required)@deleteMessage <message>- Custom error message@cascade- Auto-delete related records@withTrashed- Include soft-deleted records in checks@noCount- Disable count in error message for this relation
Note: Conditional blocking (condition callback) is not available in docblock mode for security reasons (would require eval()).
Mode Comparison: Feature Parity
Both modes support nearly all features, with one exception for security reasons:
| Feature | Explicit Mode | Docblock Mode | Example |
|---|---|---|---|
| Basic Blocking | โ | โ | Prevent deletion when relations exist |
| Custom Messages | โ | โ | 'message' => '...' vs @deleteMessage |
| Cascade Delete | โ | โ | 'cascade' => true vs @cascade |
| Include Soft Deleted | โ | โ | 'withTrashed' => true vs @withTrashed |
| Disable Count | โ | โ | 'includeCount' => false vs @noCount |
| Count Placeholder | โ | โ | Use :count in message |
| Force Delete Bypass | โ | โ | Both modes respect config |
| Conditional Blocking | โ | โ | 'condition' => fn($q) => ... only |
Why No Conditional Blocking in Docblock Mode?
Conditional blocking requires executing PHP closures/callbacks that filter the query before checking for records:
To support this in docblock mode would require either:
- Parsing and eval()ing PHP code from docblocks - Major security vulnerability
- A limited DSL for conditions - Complex to implement and restrictive
Since conditional blocking is an advanced feature and explicit mode provides a safe implementation, we've chosen to keep docblock mode simple and secure.
Recommendation: Use explicit mode if you need conditional blocking. Use docblock mode for simpler, annotation-driven configuration.
Advanced Options
Custom Error Messages
Including Record Counts
By default, error messages include the count of related records. You can customize this:
Output examples:
- With placeholder:
"Cannot delete user with 47 posts" - Auto-appended:
"User has comments (12 records)" - Disabled:
"User has comments"
You can also disable counts globally in the config file:
Including Soft Deleted Records
Cascade Delete
Automatically delete related records instead of blocking:
Note: Cascade deletes are executed before checking blockers, so you can mix cascade and blocking rules.
Conditional Blocking
Block deletion only when specific conditions are met:
The condition callback receives the relationship query builder, allowing you to apply any filters before checking if records exist.
Exception Handling
The package throws a DeletionBlockedException (extends ValidationException) when deletion is blocked:
Force Delete
When allow_force_delete is enabled in config, you can bypass guards using force delete:
Audit Command
Check what would block a deletion before attempting it:
Output:
Or if deletion is allowed:
Authorization with Policies
The package includes a DeletionPolicy class for use with Laravel Gates:
The policy provides:
delete()- Returnsfalseif deletion blockers existforceDelete()- Checks if user is admin ($user->is_admin)
Checking Blockers Programmatically
How It Works
- The trait boots during model initialization and registers a
deletingevent listener - Before deletion, it checks all configured dependencies
- For each dependency, it queries the relationship to see if related records exist
- If any blockers are found, it throws a
DeletionBlockedException - If
allow_force_deleteis enabled andforceDelete()is called, guards are bypassed
Performance Considerations
- Caching: Relation discovery is cached (when in docblock mode) to avoid repeated reflection
- Query Optimization: Only checks for existence, doesn't load full records
- Lazy Evaluation: Only checks relationships when deletion is attempted
Requirements
- PHP 8.2 or higher
- Laravel 11.0 or 12.0
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Work Done Right
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-deletion-guard with dependencies
spatie/laravel-package-tools Version ^1.16
illuminate/contracts Version ^11.0||^12.0||^13.0