createnl/expires

为数据库记录设置过期日期,如未来的删除时间列

0.3 2017-08-03 09:04 UTC

This package is not auto-updated.

Last update: 2024-09-28 09:22:27 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

一个用于向数据库记录添加过期日期的包。

安装

通过Composer

$ composer require createnl/expires

用法

你的迁移

$table->dateTime('expires_at')->nullable()->default(null);

你的模型

class Model extends Eloquent
{
    use Createnl\Expires\Expirable
    
    /**
     * Indicates if the model should set an auto expire
     *
     * @var bool
     */
    protected static $autoExpire = true;
     
    /**
     * Indicates if the model should reset the expiration date on model update
     *
     * @var bool
     */
    protected static $autoExtend = true;
    
    /**
     * The amount of interval to be added to the
     * Please see ISO_8601 durations for correct markups
     *
     * @var string (\DateInterval)
     */
    protected static $autoExpireDate = 'P5Y';
}

方法

// Get records with expired
$model->withExpired() : Builder;
 
// Get only expired records
$model->onlyExpired() : Builder;
 
// Update expiration date
$model->setExpiration(Carbon $date) : Model;
 
// Remove expiration date
$model->unExpire() : Model;
 
// Check if record is expired
$model->isExpired() : bool;
 
// Get carbon object of expiration date
$model->expiresAt() :? Carbon;
 
// Extend expiration by defined interval
$model->extendExpiration() : Model;
 
// Disable automatic setting of expiration date
Model::disableExpiring() : void;
 
// Enable automatic setting of expiration date
Model::enableExpiring() : void;

自定义过期日期逻辑

/**
 * @override
 * Get Carbon object of parsed expiration date.
 *
 * @return Carbon
 */
public function expirationDate() : Carbon
{
    // @todo: Manipulate expiration date
    $interval = new \DateInterval(self::$autoExpireDate);
    return $this->freshTimestamp()->add($interval);
}

变更日志

请参阅CHANGELOG了解最近更改的详细信息。

测试

$ composer test

贡献

请参阅CONTRIBUTINGCONDUCT获取详细信息。

安全

如果您发现任何安全相关的问题,请发送电子邮件至alex@create.nl,而不是使用问题跟踪器。

鸣谢

许可

MIT许可(MIT)。请参阅许可文件获取更多信息。