razisayyed/laravel-cascaded-soft-deletes
Composer 安装命令:
composer require razisayyed/laravel-cascaded-soft-deletes
包简介
Cascades delete/restore Soft Deletable relationships
README 文档
README
This is a Laravel 8 package for cascding SoftDeletes delete/restore actions.
- Laravel 7.0 is supported from v0.1.0 to v1.0.2
- Laravel 8.0 is supported from v0.1.0 to v1.0.2
- Laravel 9.0 is supported since v1.0.2
- Laravel 10.0 is supported since v1.0.4 (Thanks to @mojowill)
Although this project is completely free for use, I appreciate any support!
Contents:
Features
- Cascade soft delete for chosen relations
- Cascade restore for chosen relations (only models with deleted_at >= restoredInstance->deleted_at value will be restored)
- Ability to follow custom query
- By default all cascade action will be added to default queue, you can change this behaviour by publishing package's config file.
Notes
- The idea of this package has been extracted from the fabulous package laravel-nestedset
- Because the package relies on deleted_at column to make the comparision when it cascades restore action, it is recommended to use
$table->softDeletes('deleted_at', 6);in the migration files. Otherwise, you may restore a related model that has been deleted before the instance in a fraction of a second.
Installation
To install the package, in terminal:
composer require razisayyed/laravel-cascaded-soft-deletes
publish config
If you need to change config values for sync/async behaviour you may issue:
php artisan vendor:publish --provider="RaziAlsayyed\LaravelCascadedSoftDeletes\Providers\CascadedSoftDeletesProvider" --tag="config"
Setting up
to setup CascadedSoftDeletes you need to use the trait at the parent model and define $cascadedSoftDeletes property or getCascadedSoftDeletes() method.
Simple example with $cascadedSoftDeletes property
... <?php use \Illuminate\Database\Eloquent\Model; use \Illuminate\Database\Eloquent\SoftDeletes; use RaziAlsayyed\LaravelCascadedSoftDeletes\Traits\CascadedSoftDeletes; class Page extends Model { use SoftDeletes; use CascadedSoftDeletes; protected $cascadedSoftDeletes = [ 'blocks' ]; public function blocks() { return $this->hasMany(Block::class); } }
<?php use \Illuminate\Database\Eloquent\Model; use \Illuminate\Database\Eloquent\SoftDeletes; class Block extends Model { use SoftDeletes; public function page() { return $this->belongsTo(Page::class); } }
Advanced example with getCascadedSoftDeletes and custom queries
You can also define a custom query to cascade soft deletes and restores through.
the following example describes a scenario where Folder is a model that uses NodeTrait from laravel-nestedset class and each folder has many albums. getCascadedSoftDeletes() in the example will cascade soft deletes and restores to albums related to the folder and all its decendants.
... <?php use \Illuminate\Database\Eloquent\Model; use \Illuminate\Database\Eloquent\SoftDeletes; use RaziAlsayyed\LaravelCascadedSoftDeletes\Traits\CascadedSoftDeletes; class Folder extends Model { use SoftDeletes; use NodeTrait; use CascadedSoftDeletes; public function albums() { return $this->hasMany(Album::class); } protected function getCascadedSoftDeletes() { return [ 'albums' => function() { return Album::whereHas('folder', function($q) { $q->withTrashed() ->where('_lft', '>=', $this->getLft()) ->where('_rgt', '<=', $this->getRgt()); }); } ]; } }
Requirements for the Parent & Child model classes
- Both classes must use SoftDeletes trait.
- Parent class must use CascadedSoftDeletes trait.
- Parent class must define $cascadedSoftDeletes or implement getCascadedSoftDeletes method which must return a list of cascaded HasMany relations and/or custom Queries.
License
MIT License
Copyright (c) 2022 Razi Alsayyed
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
razisayyed/laravel-cascaded-soft-deletes 适用场景与选型建议
razisayyed/laravel-cascaded-soft-deletes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.07k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2022 年 01 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「orm」 「laravel」 「eloquent」 「cascade」 「soft deletes」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 razisayyed/laravel-cascaded-soft-deletes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 razisayyed/laravel-cascaded-soft-deletes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 razisayyed/laravel-cascaded-soft-deletes 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Kinikit - PHP Application development framework MVC component
Store your language lines in the database, yaml or other sources
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 4.07k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-01-20