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
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';
}
}
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}");
});