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


'connections' => [
    // 其他连接...
    
    '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', ''),
        'unix_socket' => env('ARCHIVE_DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],
],

return [
    // 默认分块大小
    'default_chunk_size' => 1000,

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

    // 模型扫描路径
    'model_paths' => [
        app_path('Models'),
    ],
];



namespace App\Models;

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

class User extends Model
{
    use Archivable;
    
    /**
     * 定义可归档的记录条件
     * 
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function archivable()
    {
        // 例如:归档超过一年未活跃的用户
        return $this->where('last_login_at', '<', now()->subYear());
    }
}



namespace App\Models;

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

class Order extends Model
{
    use DateFieldArchivable;
    
    /**
     * 获取日期字段名称
     * 
     * @return string
     */
    public function getArchiveDateField()
    {
        return 'created_at';
    }
    
    /**
     * 获取归档时间间隔
     * 
     * @return \DateInterval
     */
    public function getArchiveInterval()
    {
        return new \DateInterval('P1Y'); // 一年
    }
}



namespace App\Models;

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

class Log extends Model
{
    use MonthlyArchivable;
    
    /**
     * 获取日期字段名称
     * 
     * @return string
     */
    public function getArchiveDateField()
    {
        return 'created_at';
    }
    
    /**
     * 获取归档时间间隔(月数)
     * 
     * @return int
     */
    public function getArchiveMonths()
    {
        return 6; // 6个月前的记录将被归档
    }
}

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

// 归档符合条件的所有记录
$user->archiveAll($chunkSize = 1000);

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

Event::listen(ModelsArchived::class, function ($event) {
    // $event->model 包含被归档的模型类名
    // $event->count 包含被归档的记录数量
    
    // 执行自定义逻辑
});
bash
php artisan vendor:publish --provider="Nexusbrother\Archivable\ServiceProvider" --tag="config"
bash
php artisan model:archive-structure-sync
bash
php artisan model:archive
bash
php artisan model:archive --chunk=500
bash
php artisan model:archive --pretend