spatie / laravel-model-cleanup
此包用于删除数据库中不再需要的记录。
Requires
- php: ^7.4|^8.0
- illuminate/support: ^7.28|^8.0
Requires (Dev)
- mockery/mockery: ^1.0
- orchestra/testbench: ^5.3|^6.0
- pestphp/pest: ^0.3.0
- pestphp/pest-plugin-laravel: ^0.3.0
- phpunit/phpunit: ^9.0
- spatie/test-time: ^1.2
- vimeo/psalm: ^4.2
README
🚨 此包不再维护,我们建议使用 Laravel 内置的可修剪功能 🚨
清理不需要的记录
此包将清理旧记录。
您想要清理的模型应该有一个返回清理配置的 cleanUp
方法。以下是一个示例,所有5天前的记录都将被清理。
use Illuminate\Database\Eloquent\Model; use Spatie\ModelCleanup\CleanupConfig; use Spatie\ModelCleanup\GetsCleanedUp; class YourModel extends Model implements GetsCleanedUp { ... public function cleanUp(CleanupConfig $config): void { $config->olderThanDays(5); } }
在配置文件中注册模型后,运行 clean:models
artisan 命令将删除所有5天前创建的记录。
该包包含其他各种方法来指定哪些记录应该被删除。
支持我们
我们在创建 最佳开源包 上投入了大量资源。您可以通过 购买我们的付费产品 来支持我们。
我们非常感谢您从家乡寄来明信片,说明您正在使用我们的哪些包。您可以在 我们的联系页面 上找到我们的地址。我们将所有收到的明信片发布在我们的 虚拟明信片墙上。
安装
您可以通过 composer 安装此包。
composer require spatie/laravel-model-cleanup
接下来,您必须发布配置文件。
php artisan vendor:publish --provider="Spatie\ModelCleanup\ModelCleanupServiceProvider"
这是已发布的配置文件 model-cleanup.php
的内容。
return [ /* * All models in this array that implement `Spatie\ModelCleanup\GetsCleanedUp` * will be cleaned. */ 'models' => [ // App\Models\YourModel::class, ], ];
可选地,您可以将 Spatie\ModelCleanup\Commands\CleanUpModelsCommand
命令安排为以您想要的频率运行以清理模型。以下是一个示例,所有模型将每天午夜进行清理。
// in app/Console/Kernel.php protected function schedule(Schedule $schedule) { $schedule->command(\Spatie\ModelCleanup\Commands\CleanUpModelsCommand::class)->daily(); }
使用
您想要清理的所有模型都必须实现 GetsCleanedUp
接口。在必需的 cleanUp
方法中,您可以指定哪些记录被认为是旧的并且应该被删除。
以下是一个示例,所有5天前的记录都将被清理。
use Illuminate\Database\Eloquent\Model; use Spatie\ModelCleanup\CleanupConfig; use Spatie\ModelCleanup\GetsCleanedUp; class YourModel extends Model implements GetsCleanedUp { ... public function cleanUp(CleanupConfig $config): void { $config->olderThanDays(5); } }
接下来,您应该在 model-cleanup
配置文件的 models
键中注册此模型。
// in config/model-cleanup.php return [ 'models' => [ App\Models\YourModel::class, ], // ... ]
当运行控制台命令 clean:models
时,所有5天前的模型将被删除。
软删除模型
此包还支持清理启用了软删除功能的模型。使用 Illuminate\Database\Eloquent\SoftDeletes
特性并且被认为是旧的模型,将从数据库中永久删除,而不是被标记为已删除。
在 CleanupConfig
中可用的方法
olderThanDays
使用此方法,您可以标记具有超过给定天数created_at
值的记录为过时。
以下是一个示例,其中所有超过5天的模型都被认为是过时的。
public function cleanUp(CleanupConfig $config): void { $config->olderThanDays(5); }
olderThan
olderThan
方法接受一个Carbon
实例。所有在实例之前的created_at
值都将被认为是过时的。
以下是一个示例,其中所有超过一年的模型都被认为是过时的。
public function cleanUp(CleanupConfig $config): void { $config->olderThan(now()->subYear()); }
useDateAttribute
当使用olderThanDays
和olderThan
方法时,后台构建的删除查询将使用created_at
列。您可以使用useDateAttribute
方法指定一个替代列。
public function cleanUp(CleanupConfig $config): void { $config ->olderThanDays(5) ->useDateAttribute('custom_date_column'); }
scope
使用scope
方法可以使删除旧记录的查询更具体。
假设您的模型有一个status
属性。只有状态为inactive
的记录才可能被清理。以下是一个示例,其中所有状态为inactive
且超过5天的记录都将被清理。
public function cleanUp(CleanupConfig $config): void { $config ->olderThanDays(5) ->scope(fn (Illuminate\Database\Eloquent\Builder $query) => $query->where('status', 'inactive')); }
chunk
默认情况下,模型通过执行单个delete
查询来清理。当您想要清理一个非常大的表时,这个单个查询可能会锁定您的表很长时间。甚至可能根本无法获得锁。
为了解决这个问题,该包可以使用chunk
方法分批删除记录。
在这个示例中,所有超过5天的记录将以1000条记录为一组被删除。
public function cleanUp(CleanupConfig $config): void { $config ->olderThanDays(5) ->chunk(1000); }
当没有更多需要删除的记录时,包将停止删除记录。
如果您需要更细粒度地控制何时停止删除,您可以将闭包作为chunk
的第二个参数传递。在闭包中返回false
将停止删除过程。
在下面的示例中,删除过程将继续进行,直到所有超过5天的记录都被删除或模型记录数低于5000。
public function cleanUp(CleanupConfig $config): void { $config ->olderThanDays(5) ->chunk(1000, fn() => YourModel::count() > 5000); }
事件
在模型被清理后,即使没有记录被删除,也会触发Spatie\ModelCleanup\Events\ModelCleanedUp
事件。
它有两个公共属性:包含被清理的模型实例的model
,以及删除的记录数numberOfDeletedRecords
。
变更日志
请参阅CHANGELOG获取有关最近更改的更多信息。
测试
composer test
贡献
请参阅CONTRIBUTING获取详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件freek@spatie.be而不是使用问题跟踪器。
Postcardware
您可自由使用此软件包,但如果它进入了您的生产环境,我们非常感激您从家乡寄给我们一张明信片,并注明您正在使用我们哪个软件包。
我们的地址是:Spatie,Kruikstraat 22,2018 安特卫普,比利时。
我们将所有收到的明信片发布在我们的公司网站上。
致谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。