PHPackages                             gigerit/laravel-cascade-delete - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. gigerit/laravel-cascade-delete

ActiveLibrary[Database &amp; ORM](/categories/database)

gigerit/laravel-cascade-delete
==============================

Smart cascading deletes for Laravel Eloquent with support for soft deletes, polymorphic relations, and automatic detaching.

v1.3.2(1mo ago)42351[1 PRs](https://github.com/gigerIT/laravel-cascade-delete/pulls)MITPHPPHP ^8.2CI passing

Since Jan 12Pushed 1mo agoCompare

[ Source](https://github.com/gigerIT/laravel-cascade-delete)[ Packagist](https://packagist.org/packages/gigerit/laravel-cascade-delete)[ Docs](https://github.com/gigerit/laravel-cascade-delete)[ GitHub Sponsors](https://github.com/gigerit)[ RSS](/packages/gigerit-laravel-cascade-delete/feed)WikiDiscussions main Synced today

READMEChangelog (10)Dependencies (24)Versions (19)Used By (0)

Laravel Cascade Delete
======================

[](#laravel-cascade-delete)

[![Latest Version on Packagist](https://camo.githubusercontent.com/95cc6842801d527f578e7cacad3d6616da9354053a4be3007b1a6a87be27218d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f676967657269742f6c61726176656c2d636173636164652d64656c6574652e7376673f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/gigerit/laravel-cascade-delete)[![GitHub Tests Action Status](https://camo.githubusercontent.com/3f58d57b5f6bcba5b62c0f2a06a9a9589c2b4516565d09996b8020a22efa6fd5/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f676967657269742f6c61726176656c2d636173636164652d64656c6574652f43492e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666f722d7468652d6261646765)](https://github.com/gigerit/laravel-cascade-delete/actions?query=workflow%3ACI+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/356d7110ddcc8b6ec4b65dd81a893b3c0fb9f6b4af9decdb6e4491714c5df5f7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f676967657269742f6c61726176656c2d636173636164652d64656c6574652e7376673f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/gigerit/laravel-cascade-delete)

Smart cascading deletes for Laravel Eloquent with support for soft deletes, polymorphic relations, and automatic detaching.

The main advantage of this package over others is its **unified approach**. While many packages handle only soft deletes or only standard relations, this package provides a single trait that intelligently manages all variants:

- **Standard Relations** (`HasOne`, `HasMany`)
- **Soft Deletes** (Recursive soft/hard deletion)
- **Polymorphic Relations** (`MorphOne`, `MorphMany`)
- **Many-to-Many Relations** (`BelongsToMany`, `MorphToMany`) via automatic detaching
- **Orphan Cleanup**: Tools to clean residual polymorphic records after bulk deletions

Features
--------

[](#features)

- **All-in-One Trait**: Handles all relationship types in a single implementation.
- **Transaction Safety**: All cascading operations are wrapped in a database transaction to ensure atomicity.
- **Integrity Verification**: Verifies that the number of deleted or detached records matches expectations.
- **Intelligent Detaching**: Automatically calls `detach()` for many-to-many relations instead of deleting the related models.
- **Recursive Force Deleting**: Correctly handles `forceDelete()` by propagating it to related models, even those using soft deletes.

Requirements
------------

[](#requirements)

- PHP 8.2 or higher
- Laravel 11.x or higher

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require gigerit/laravel-cascade-delete
```

Usage
-----

[](#usage)

Simply add the `CascadeDeletes` trait to your model and define the `$cascadeDeletes` property:

```
namespace App\Models;

use Gigerit\LaravelCascadeDelete\Concerns\CascadeDeletes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model
{
    use SoftDeletes;
    use CascadeDeletes;

    protected $cascadeDeletes = [
        'posts',      // HasMany
        'profile',    // HasOne
        'roles',      // BelongsToMany (will detach)
        'comments',   // MorphMany
    ];

    public function posts()
    {
        return $this->hasMany(Post::class);
    }

    public function profile()
    {
        return $this->hasOne(Profile::class);
    }

    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }

    public function comments()
    {
        return $this->morphMany(Comment::class, 'commentable');
    }
}
```

### How it works

[](#how-it-works)

- **Delete / Soft Delete**: When you call `$model->delete()`, the package will iterate through the defined relations. If the child models support soft deletes, they will be soft deleted. If not, they will be hard deleted.
- **Force Delete**: If you call `$model->forceDelete()`, the package will automatically use `withTrashed()` on the relations to find and permanently delete all related records.
- **Many-to-Many**: For `BelongsToMany` or `MorphToMany` relations, the package will call `detach()` on the relationship, ensuring the pivot records are removed without deleting the actual related models.
- **Transactions**: If any deletion fails or the record count doesn't match, the entire operation is rolled back.

Handling Orphan Polymorphic Relations
-------------------------------------

[](#handling-orphan-polymorphic-relations)

Bulk deletes in Laravel (e.g., `User::where('active', false)->delete()`) do not fire model events, which can leave orphaned records in polymorphic relationships. This package provides tools to identify and clean these up.

### Artisan Command

[](#artisan-command)

You can clean up all orphaned polymorphic relations defined in your models:

```
php artisan cascade-delete:clean
```

Use the `--dry-run` flag to see how many records would be deleted without actually removing them:

```
php artisan cascade-delete:clean --dry-run
```

### Manual Cleanup

[](#manual-cleanup)

You can also trigger cleanup for a specific model class:

```
(new User)->clearOrphanMorphRelations();
```

### Configuration

[](#configuration)

You can publish the config file to customize where the package searches for models:

```
php artisan vendor:publish --tag="cascade-delete-config"
```

The default configuration looks for models in `app/`:

```
return [
    'models_paths' => [
        app_path(),
    ],
];
```

Handling Failures
-----------------

[](#handling-failures)

If the number of records deleted or detached does not exactly match the number of records found in the relationship, a `\LogicException` will be thrown, and the database transaction will be rolled back.

This prevents silent failures where some related records might have been left orphaned due to database constraints or other issues.

Testing
-------

[](#testing)

```
composer test
```

Credits
-------

[](#credits)

- [gigerit](https://github.com/gigerit)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

46

—

FairBetter than 92% of packages

Maintenance88

Actively maintained with recent releases

Popularity20

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 76.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~11 days

Recently: every ~27 days

Total

11

Last Release

59d ago

PHP version history (2 changes)v1.0.0PHP ^8.4

v1.2.1PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/6b8b982f1ca65bca2356c040bc612db0d03ee2443fd194bdfa9f44a4c3fa2267?d=identicon)[gigerit](/maintainers/gigerit)

---

Top Contributors

[![gigerIT](https://avatars.githubusercontent.com/u/64487711?v=4)](https://github.com/gigerIT "gigerIT (66 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (12 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (8 commits)")

---

Tags

laravelgigerITlaravel-cascade-delete

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/gigerit-laravel-cascade-delete/health.svg)

```
[![Health](https://phpackages.com/badges/gigerit-laravel-cascade-delete/health.svg)](https://phpackages.com/packages/gigerit-laravel-cascade-delete)
```

###  Alternatives

[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k4.8M47](/packages/spatie-laravel-pdf)[dedoc/scramble

Automatic generation of API documentation for Laravel applications.

2.1k11.2M101](/packages/dedoc-scramble)[wnx/laravel-backup-restore

A package to restore database backups made with spatie/laravel-backup.

213421.2k2](/packages/wnx-laravel-backup-restore)[spatie/laravel-passkeys

Use passkeys in your Laravel app

471890.7k39](/packages/spatie-laravel-passkeys)[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

3914.6k](/packages/rawilk-profile-filament-plugin)[lacodix/laravel-model-filter

A Laravel package to filter, search and sort models with ease while fetching from database.

17558.6k](/packages/lacodix-laravel-model-filter)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
