hith/laravel-eraser
Composer 安装命令:
composer require hith/laravel-eraser
包简介
A Laravel package for erasing models and related data safely.
README 文档
README
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
| Laravel Version | Eraser Version |
|---|---|
| 12.x | 1.0+ |
Getting Started
Requires:
1: First, use Composer 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:
-there are two types of traits [manual , auto]:
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:
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:
- 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:
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
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
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
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
Thank you for considering contributing to Laravel Eraser. All the contribution guidelines are mentioned here.
License
Laravel Eraser is an open-sourced software licensed under the MIT license.
hith/laravel-eraser 适用场景与选型建议
hith/laravel-eraser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「relations」 「delete」 「eloquent」 「cleanup」 「eraser」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hith/laravel-eraser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hith/laravel-eraser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hith/laravel-eraser 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Eloquent model updater for nested relations data for Laravel.
Missing relationship events for Laravel
Laravel package for inspecting model classes on relations en attributes.
SoftDelete plugin for CakePHP
Laravel console command for deleting temp files and associated model instances.
Work with relations as simple as with attributes.
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-23