PHPackages                             hith/laravel-eraser - 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. hith/laravel-eraser

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

hith/laravel-eraser
===================

A Laravel package for erasing models and related data safely.

v1.0.0(5mo ago)01MITPHPPHP ^8.2CI passing

Since Nov 24Pushed 5mo agoCompare

[ Source](https://github.com/hith-hj/laravel-eraser)[ Packagist](https://packagist.org/packages/hith/laravel-eraser)[ RSS](/packages/hith-laravel-eraser/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (5)Versions (2)Used By (0)

---

Laravel Eraser
--------------

[](#laravel-eraser)

Laravel Eraser focuses on **Erasing Eloquent Model Relation**. It has two modes of operation.

- Manual: this mode require the developer to define erasable array contains relations should be deleted
- Auto: this mode use auto-discover for relations to be deleted

### Supported Laravel versions

[](#supported-laravel-versions)

Laravel VersionEraser Version12.x1.0+Getting Started
---------------

[](#getting-started)

> **Requires:**

- **[PHP 8.2+](https://php.net/releases/)**
- **[Laravel 12.x+](https://github.com/laravel/laravel)**

**1**: First, use [Composer](https://getcomposer.org) to install laravel-eraser into your project:

```
composer require "hith/laravel-eraser"
```

**2**: Then, publish config file:

```
php artisan vendor:publish --tag=eraser-config
```

**3**: Finally, Use the package in two ways:

- By adding traits directly to your models.
- By using the Eraser class to delete/clean models.

### Usage of traits:

[](#usage-of-traits)

-there are two types of traits \[manual , auto\]:

#### Manual Eraser Trait:

[](#manual-eraser-trait)

```
use Eraser\Traits\HasManualEraser;

class Post extends Model
{
    use HasManualEraser;

    public array $erasable = ['comments','tags'];

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function comments(): HasMany
    {
        return $this->hasMany(Comment::class);
    }

    public function tags(): BelongsToMany
    {
        return $this->belongsToMany(Comment::class);
    }
}
```

Then instead of:

```
$post->comments()->delete();
$post->tags()->detach();
$post->delete();
```

Simply do:

```
$post->delete();
```

Or, if you just want to clear relations without deleting the model:

```
$post->clean();
```

#### Auto Eraser Trait:

[](#auto-eraser-trait)

```
use Eraser\Traits\HasAutoEraser;

class Post extends Model
{
    use HasAutoEraser;

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function comments(): HasMany
    {
        return $this->hasMany(Comment::class);
    }

    public function tags(): BelongsToMany
    {
        return $this->belongsToMany(Comment::class);
    }
}
```

As with the manual trait, simply do:

```
$post->clean();
// or
$post->delete();
```

> The user() relation will not be deleted in either mode, as it is considered a parent relation.

### Usage of Eraser Class:

[](#usage-of-eraser-class)

- Manual Mode (default):

```
use Eraser\Eraser;

$eraser = new Eraser();    // Manual is the default
$eraser->clean($model);    // cleans relations only
$eraser->delete($model);   // or cleans relations and deletes model
```

- Auto Mode

```
use Eraser\Eraser;

$eraser = new Eraser('auto');    // set eraser to auto
$eraser->clean($model);
$eraser->delete($model);
```

### Usage of Eraser Facade:

[](#usage-of-eraser-facade)

```
use Eraser\Facades\Erase;

Erase::clean($model);   // Manual is the default
Erase::delete($model);  // or cleans relations and deletes model

// Erase Facade default mode is manual
// use `type()` method to set type
// types: [manual,auto]
Erase::type('auto')->clean($model);
```

Eraser Configuration
--------------------

[](#eraser-configuration)

You can alter the behavior of the eraser either globally using the eraser config file or by defining some of this attributes on the model.

```
/**
* Determines whether the "eraser" should automatically start deleting
* related models when the parent model is being deleted.
* Default: true
* */
public bool $eraser_onDeleteStart = true;

/**
 * Controls whether relations to be deleted should be automatically
 * discovered when using the Auto Eraser functionality.
 * Default: true
 * */
public bool $eraser_autoDiscover = true;

/**
 * Enables or disables logging for eraser operations.
 * Default: true
 * */
public bool $eraser_logging = false;

/**
 * Set logger for model logs
 * */
public callable|LoggerInterface $logger;
```

Best practices
--------------

[](#best-practices)

Logging: Keep logging enabled in development/staging;

Testing: Validate deletion flows, including parent relations and many-to-many detaches, before production.

Deny list: Extend eraser\_base\_deny\_list to match your naming patterns for utility methods.

Mode choice: Use manual mode for sensitive domains (users, orders); auto mode for convenience on simpler models.

Ownership safety: Parent relations (BelongsTo, MorphTo) are skipped to preserve ownership chains and to prevent circular deletion loops.

Pivot hygiene: Many-to-many are detached; ensure pivot constraints and cascades are correct.

Model Relationships:

In order for Eraser (auto-discover) to recognize Model relationships the following is recommended:

- the return type must be defined
- the method must be `public`
- the method has no arguments

```
public function user(): BelongsTo
{
    return $this->belongsTo(User::class);
}

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

public function tags(): BelongsToMany
{
    return $this->belongsToMany(Tag::class);
}
```

Log Outputs Example
-------------------

[](#log-outputs-example)

```
Info
[info] Processing: Post[42]
[info] Processing relation 'comments'
[info] Relation 'comments' processed

Skipped
[info] Skipping parent relation 'author'
[info] Skipping already processed Post:42

```

---

```
Warning
[warning] Missing 'erasable' on Post
[warning] Method 'likes' did not return a Relation

```

---

```
Error
[error] Relation 'tags' not found on Post
[error] Error deleting relation 'images': Call to undefined method

```

Contributing
------------

[](#contributing)

Thank you for considering contributing to Laravel Eraser. All the contribution guidelines are mentioned [here](CONTRIBUTING.md).

License
-------

[](#license)

Laravel Eraser is an open-sourced software licensed under the [MIT license](LICENSE.md).

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance70

Regular maintenance activity

Popularity1

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 88.2% 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

Unknown

Total

1

Last Release

167d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/824961a563ed0c70aa30c15d1e1823448d21fc7bc7f1832451b7caf413a894cc?d=identicon)[hith-hj](/maintainers/hith-hj)

---

Top Contributors

[![hith-hj](https://avatars.githubusercontent.com/u/52906481?v=4)](https://github.com/hith-hj "hith-hj (15 commits)")[![semantic-release-bot](https://avatars.githubusercontent.com/u/32174276?v=4)](https://github.com/semantic-release-bot "semantic-release-bot (2 commits)")

---

Tags

laraveleloquentrelationsdeletecleanuperaser

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/hith-laravel-eraser/health.svg)

```
[![Health](https://phpackages.com/badges/hith-laravel-eraser/health.svg)](https://phpackages.com/packages/hith-laravel-eraser)
```

###  Alternatives

[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[highsolutions/eloquent-sequence

A Laravel package for easy creation and management sequence support for Eloquent models with elastic configuration.

121130.3k](/packages/highsolutions-eloquent-sequence)[korridor/laravel-has-many-sync

Laravel has many sync

3578.1k](/packages/korridor-laravel-has-many-sync)[cesargb/laravel-cascade-delete

Cascading eliminations implemented in polymorphic relationships for the Laravel apps

1956.1k1](/packages/cesargb-laravel-cascade-delete)[toponepercent/baum

Baum is an implementation of the Nested Set pattern for Eloquent models.

3154.7k](/packages/toponepercent-baum)[vectorial1024/laravel-cache-evict

Efficiently remove expired Laravel file/database cache data

5813.2k](/packages/vectorial1024-laravel-cache-evict)

PHPackages © 2026

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