定制 maize-tech/laravel-model-expires 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

maize-tech/laravel-model-expires

Composer 安装命令:

composer require maize-tech/laravel-model-expires

包简介

Laravel Model Expires

README 文档

README

Social Card of Laravel Model Expires

Laravel Model Expires

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

With this package you can add expiration date to any model and exclude expired models from queries. When needed, you could send a notification for expiring models. You can also set a deletion date for every model and automatically clean them up with a command.

Installation

You can install the package via composer:

composer require maize-tech/laravel-model-expires

You can publish the config and migration files and run the migrations with:

php artisan model-expires:install

This is the contents of the published config file:

return [

    /*
    |--------------------------------------------------------------------------
    | Expiration model
    |--------------------------------------------------------------------------
    |
    | Here you may specify the fully qualified class name of the expiration model.
    |
    */

    'expiration_model' => Maize\ModelExpires\Models\Expiration::class,

    'model' => [

        /*
        |--------------------------------------------------------------------------
        | Expires after days
        |--------------------------------------------------------------------------
        |
        | Here you may specify the default amount of days after which a model
        | should expire.
        | If null, all newly created models won't have a default expiration date.
        |
        */

        'expires_after_days' => null,

        /*
        |--------------------------------------------------------------------------
        | Deletes after days
        |--------------------------------------------------------------------------
        |
        | Here you may specify the default amount of days after which a model
        | should be deleted.
        | If null, all newly created models won't have a default deletion date.
        |
        */

        'deletes_after_days' => null,
    ],

    'expiring_notification' => [

        /*
        |--------------------------------------------------------------------------
        | Enable expiring notification
        |--------------------------------------------------------------------------
        |
        | Here you may specify whether you want to enable model expiring
        | notifications or not.
        |
        */

        'enabled' => true,

        /*
        |--------------------------------------------------------------------------
        | Notification class
        |--------------------------------------------------------------------------
        |
        | Here you may specify the fully qualified class name of the default notification.
        | If null, no notifications will be sent.
        |
        */

        'notification' => Maize\ModelExpires\Notifications\ModelExpiringNotification::class,

        /*
        |--------------------------------------------------------------------------
        | Notifiable emails
        |--------------------------------------------------------------------------
        |
        | Here you may specify the default list of notifiable email addresses.
        |
        */

        'notifiables' => [
            //
        ],
    ],
];

Usage

Basic

To use the package, add the Maize\ModelExpires\HasExpiration trait to all models you want to have an expiration date:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Maize\ModelExpires\HasExpiration;

class User extends Model
{
    use HasExpiration;
}

That's it! All you have to do from now on is calling the setExpiresAt method every time you want to set an expiration and/or deletion date:

$user = User::create([])->setExpiresAt(
    expiresAt: now()->addDays(5),
    deletesAt: now()->addDays(10),
); // user will have both an expiration and deletion date

$user = User::create([])->setExpiresAt(
    expiresAt: now()->addDays(5)
); // user will have an expiration date but will not be deleted

Checking expiration and deletion days left

You can also check whether a model is expired and calculate the amount of days before its expiration (or deletion):

$user = User::create([])->setExpiresAt(
    expiresAt: now()->addDays(5),
    deletesAt: now()->addDays(10),
);

$user->isExpired(); // returns false

$user->getDaysLeftToExpiration(); // returns 5
$user->getDaysLeftToDeletion(); // returns 10


$user = User::create([])->setExpiresAt(
    expiresAt: now()->subDay()
);

$user->isExpired(); // returns true

$user->getDaysLeftToExpiration(); // returns 0, as the model is already expired
$user->getDaysLeftToDeletion(); // returns null, as model does not have a deletion date

Excluding expired models

When you want to exclude expired models, all you have to do is use the withoutExpired scope method:

$user = User::create([]); // user does not have an expiration date
$expiredUser = User::create([])->setExpiresAt(
    expiresAt: now()->subDay(),
); // user is already expired

User::withoutExpired()->count(); // returns 1, which is the $user model
User::withoutExpired()->get(); // returns the $user model

Retrieving only expired models

When you want to retrieve expired models, all you have to do is use the onlyExpired scope method:

$user = User::create([]); // user does not have an expiration date
$expiredUser = User::create([])->setExpiresAt(
    expiresAt: now()->subDay(),
); // user is already expired

User::onlyExpired()->count() // returns 1, which is the $expiredUser model
User::onlyExpired()->get(); // returns the $expiredUser model

Default expiration date

If you wish, you can define a default expiration date. This can be done in two ways.

First, you can set a value for expires_after_days property under config/model-expires.php config file. When set, all models including the Maize\ModelExpires\HasExpiration trait will automatically have an expiration date upon its creation:

config()->set('model-expires.model.expires_after_days', 5);

$user = User::create([]);
$user->getDaysLeftToExpiration(); // returns 5

The second way is overriding the defaultExpiresAt method within all models you want to have a default expiration date:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Maize\ModelExpires\HasExpiration;

class User extends Model
{
    use HasExpiration;
    
    protected static function defaultExpiresAt(): ?Carbon
    {
        return now()->addDays(10); // all user models will expire 10 days after being created
    }
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Maize\ModelExpires\HasExpiration;

class Tenant extends Model
{
    use HasExpiration;
    
    protected static function defaultExpiresAt(): ?Carbon
    {
        return now()->addMonth(); // all tenant models will expire 1 month after being created
    }
}

Default deletion date

If you wish, you can define a default deletion date. This can be done in two ways.

First, you can set a value for deletes_after_days property under config/model-expires.php config file. When set, all models including the Maize\ModelExpires\HasExpiration trait will automatically have a deletion date upon its creation:

config()->set('model-expires.model.deletes_after_days', 5);

$user = User::create([]);
$user->getDaysLeftToDeletion(); // returns 5

The second way is overriding the defaultDeletesAt method within all models you want to have a default deletion date:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Maize\ModelExpires\HasExpiration;

class User extends Model
{
    use HasExpiration;
    
    protected static function defaultDeletesAt(): ?Carbon
    {
        return now()->addDays(10); // all user models will be deleted 10 days after being created
    }
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Maize\ModelExpires\HasExpiration;

class Tenant extends Model
{
    use HasExpiration;
    
    protected static function defaultDeletesAt(): ?Carbon
    {
        return now()->addMonth(); // all tenant models will be deleted 1 month after being created
    }
}

Scheduling expiration check

The package comes with the expires:check command, which automatically fires a ModelExpiring event for all expiring models.

To do so, you should define how often you want to fire the event. All you have to do is overriding the fireExpiringEventBeforeDays for all models using the HasExpiration trait:

use Illuminate\Database\Eloquent\Factories\HasFactory;

class User extends Authenticatable
{
    use HasExpiration;

    public static function fireExpiringEventBeforeDays(): array
    {
        return [5, 10]; // expiring events will be fired 5 and 10 days before each model's expiration
    }
}

By default, the method returns an empty array, meaning models will never fire expiring events.

Once done, you can schedule the command on a daily basis using the schedule method of the console kernel (usually located under the App\Console directory):

use Maize\ModelExpires\Commands\ModelExpiresDeleteCommand;

$schedule->command(ModelExpiresCheckCommand::class)->daily();

Scheduling models deletion

The package also comes with the expires:delete command, which automatically deletes all expired and deletable models. This comes pretty useful when automatizing its execution using Laravel's scheduling. All you have to do is add the following instruction to the schedule method of the console kernel (usually located under the App\Console directory):

use Maize\ModelExpires\Commands\ModelExpiresDeleteCommand;

$schedule->command(ModelExpiresDeleteCommand::class)->daily();

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

maize-tech/laravel-model-expires 适用场景与选型建议

maize-tech/laravel-model-expires 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 850 次下载、GitHub Stars 达 23, 最近一次更新时间为 2023 年 11 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「model」 「laravel」 「Expires」 「maize-tech」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 maize-tech/laravel-model-expires 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 maize-tech/laravel-model-expires 我们能提供哪些服务?
定制开发 / 二次开发

基于 maize-tech/laravel-model-expires 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 850
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 23
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 23
  • Watchers: 5
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-09