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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-deletion-guard

Laravel Deletion Guard

Latest Version on Packagist Total Downloads

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

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)

Docblock Mode

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:

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:

  1. Parsing and eval()ing PHP code from docblocks - Major security vulnerability
  2. 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:

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:

Checking Blockers Programmatically

How It Works

  1. The trait boots during model initialization and registers a deleting event listener
  2. Before deletion, it checks all configured dependencies
  3. For each dependency, it queries the relationship to see if related records exist
  4. If any blockers are found, it throws a DeletionBlockedException
  5. If allow_force_delete is enabled and forceDelete() is called, guards are bypassed

Performance Considerations

Requirements

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

License

The MIT License (MIT). Please see License File for more information.


All versions of laravel-deletion-guard with dependencies

PHP Build Version
Package Version
Requires php Version ^8.4
spatie/laravel-package-tools Version ^1.16
illuminate/contracts Version ^11.0||^12.0||^13.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package workdoneright/laravel-deletion-guard contains the following files

Loading the files please wait ...