PHP code example of nexusbrother / laravel-archivable

1. Go to this page and download the library: Download nexusbrother/laravel-archivable 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/ */

    

nexusbrother / laravel-archivable example snippets


'archive' => [
    'driver'   => 'mysql',
    'host'     => env('ARCHIVE_DB_HOST', '127.0.0.1'),
    'port'     => env('ARCHIVE_DB_PORT', '3306'),
    'database' => env('ARCHIVE_DB_DATABASE', 'archive'),
    'username' => env('ARCHIVE_DB_USERNAME', 'root'),
    'password' => env('ARCHIVE_DB_PASSWORD', ''),
    'charset'  => 'utf8mb4',
    'collation'=> 'utf8mb4_unicode_ci',
    'prefix'   => '',
    'strict'   => true,
    'engine'   => null,
],

return [
    // 是否启用定时归档任务
    'enable' => env('ARCHIVE_ENABLE', false),

    // 定时任务执行时间
    'schedule_daily_at' => [
        'archive_structure_sync' => '09:00',  // 表结构同步
        'archive'                => '09:10',  // 数据归档
    ],

    // 每批处理的记录数
    'default_chunk_size' => 1000,

    // 归档数据库连接名
    'db' => env('ARCHIVE_DB_CONNECTION', 'archive'),

    // 自动扫描 Archivable 模型的目录
    'model_paths' => [
        app_path('Models'),
    ],
];



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Nexusbrother\Archivable\DateFieldArchivable;

class Order extends Model
{
    use DateFieldArchivable;

    /**
     * 可选:自定义日期字段(默认 created_at)
     */
    public function getDateField(): string
    {
        return 'created_at';
    }

    /**
     * 可选:自定义归档时间界限(默认 6 个月前)
     */
    public function getDateLimit()
    {
        return today()->subYear();
    }

    /**
     * 可选:自定义归档目标表名(默认与源表同名)
     */
    public function getDestinationTable(): string
    {
        return $this->getTable() . '_' . now()->format('Ym');
    }
}



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Nexusbrother\Archivable\MonthlyArchivable;

class Log extends Model
{
    use MonthlyArchivable;

    /**
     * 可选:自定义日期字段(默认 created_at)
     */
    public function getDateField(): string
    {
        return 'created_at';
    }

    /**
     * 可选:归档多少个月之前的数据(默认 6 个月)
     */
    public function getArchiveMonthLimit()
    {
        return today()->subMonths(3);
    }
}



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Nexusbrother\Archivable\Archivable;

class User extends Model
{
    use Archivable;

    /**
     * 定义可归档的记录查询
     */
    public function archivable()
    {
        return $this->where('last_login_at', '<', now()->subYear());
    }

    /**
     * 可选:自定义归档目标表
     */
    public function getDestinationTable(): string
    {
        return 'archived_users';
    }
}

// 归档单个记录
$order->archive();

// 归档所有符合条件的记录
$order->archiveAll();

// 自定义 chunk 大小
$order->archiveAll(500);

// 带进度回调(每处理完一个 chunk 触发)
$order->archiveAll(1000, function (int $chunkCount) {
    // 处理了 $chunkCount 条记录
});

// 带开始回调和进度回调
$order->archiveAll(1000, function (int $chunkCount) {
    // 每批完成
}, function (int $total) {
    // 开始归档,共 $total 条待处理
});

use Illuminate\Support\Facades\Event;
use Nexusbrother\Archivable\ModelsArchived;

Event::listen(ModelsArchived::class, function (ModelsArchived $event) {
    // $event->model  被归档的模型类名
    // $event->count  归档的记录数量

    Log::info("Archived {$event->count} records from {$event->model}");
});
bash
php artisan vendor:publish --provider="Nexusbrother\Archivable\ServiceProvider" --tag="config"
bash
# 同步所有 Archivable 模型
php artisan model:archive-structure-sync

# 指定模型
php artisan model:archive-structure-sync --model="App\Models\Order"

# 排除模型
php artisan model:archive-structure-sync --except="App\Models\User"
bash
# 归档所有已注册模型
php artisan model:archive

# 指定模型
php artisan model:archive --model="App\Models\Order" --model="App\Models\Log"

# 排除模型
php artisan model:archive --except="App\Models\User"

# 自定义 chunk 大小
php artisan model:archive --chunk=500

# 仅预览,不执行
php artisan model:archive --pretend