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\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个月前的记录将被归档
}
}