pellesam/laravel-cascade-soft-delete-and-restore
Composer 安装命令:
composer require pellesam/laravel-cascade-soft-delete-and-restore
包简介
Cascading delete and restore for Eloquent models that implement soft delete
README 文档
README
Introduction
In scenarios when you delete a parent record - say for example a blog post - you may want to also delete any comments associated with it as a form of self-maintenance of your data.
Normally, you would use your database's foreign key constraints, adding an ON DELETE CASCADE rule to the foreign key
constraint in your comments table.
It may be useful to be able to restore a parent record after it was deleted. In those instances, you may reach for Laravel's soft deleting functionality.
In doing so, however, you lose the ability to use the cascading delete functionality that your database would otherwise
provide. That is where this package aims to bridge the gap in functionality when using the SoftDeletes trait.
Code Samples
<?php namespace App; use App\Comment; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; class Post extends Model { use SoftDeletes, CascadeSoftDeleteAndRestore; protected $cascadeRelations = ['comments']; protected $dates = ['deleted_at']; public function comments() { return $this->hasMany(Comment::class); } }
Now you can delete an App\Post record, and any associated App\Comment records will be deleted. If the App\Comment
record implements the CascadeSoftDeleteAndRestore trait as well, it's children will also be deleted and so on.
$post = App\Post::find($postId) $post->delete(); // Soft delete the post, which will also trigger the delete() method on any comments and their children.
Note: It's important to know that when you cascade your soft deleted child records, there is no way to know which were deleted by the cascading operation, and which were deleted prior to that. This means that when you restore the blog post, the associated comments will not be.
Because this trait hooks into the deleting Eloquent model event, we can prevent the parent record from being deleted
as well as any child records, if any exception is triggered. A LogicException will be triggered if the model does not
use the Illuminate\Database\Eloquent\SoftDeletes trait, or if any of the defined cascadeDeletes relationships do not
exist, or do not return an instance of Illuminate\Database\Eloquent\Relations\Relation.
Additional Note: If you already have existing event listeners in place for a model that is going to cascade soft deletes, you can adjust the priority or firing order of events to have CascadeSoftDeleteAndRestore fire after your event. To do this you can set the priority of your deleting event listener to be 1.
MODEL::observe( MODELObserver::class, 1 ); The second param is the priority.
MODEL::deleting( MODELObserver::class, 1 );
As of right now this is not documented in the Larvel docs, but just know that the default priority is 0 for all
listeners, and that 0 is the lowest priority. Passing a param of greater than 0 to your listener will cause your
listener to fire before listeners with default priority of 0
Installation
This trait is installed via Composer. To install, simply add to your composer.json file:
$ composer require pellesam/laravel-cascade-soft-delete-and-restore
pellesam/laravel-cascade-soft-delete-and-restore 适用场景与选型建议
pellesam/laravel-cascade-soft-delete-and-restore 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 03 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 pellesam/laravel-cascade-soft-delete-and-restore 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pellesam/laravel-cascade-soft-delete-and-restore 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 21
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-23