PHP code example of mckue / laravel-clear-table

1. Go to this page and download the library: Download mckue/laravel-clear-table library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mckue / laravel-clear-table example snippets


laravel > 5.8
php >= 7.3

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


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

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=时间段间隔单位