mckue/laravel-clear-table

Clear log table data through personalized configuration

v1.0.3 2023-11-04 10:12 UTC

This package is auto-updated.

Last update: 2024-06-04 11:20:45 UTC


README

通过配置的方式只需要一个命令可以清理不同的数据表

要求

laravel > 5.8
php >= 7.3

安装

composer require mckue/laravel-clear-table

生成配置文件

php artisan vendor:publish --tag=clear-tables

将会生成一个config/clear-tables.php配置文件

编写配置

<?php
return [
    //示例
    [
	 //批次处理删除n条数据
      	'chunk_size'=>2000,
         //模型类 或者 表名
    	'model'        => YourModel::class,
         //是否优化表
    	'is_optimized' => true,
         //表条件
    	'conditions'   => function ($query) {
    		$query->where('type', 1);
    		// 你可以在这里加入任何其他复杂的查询逻辑....
    	},
         //通过时间字段分段删除(这个字段要求为索引类型的时间字段,否则效果适得其反)
    	'date_column' =>'created_at',
    ],
];

执行命令 or 定时任务

执行命令会跑配置项的所有的表

php artisan clear-tables

指定表清理

php artisan clear-tables --table=your_table

清理参数

php artisan clear-tables {startDay=40} {endDay=30} {interval=10} {unit=day} {--table=}

startDay=生成时间段的开始天数 当前时间往前推n天
endDay=生成时间段的结束天数 (如果您要保留30天的数据则这个必须>=30天)
interval=时间段间隔
unit=时间段间隔单位

定时任务

 //指定表
 $schedule->command('clear-table --table=your_table', '* 5 * * *');

 //不指定表
 $schedule->command('clear-table', '* 5 * * *');