maize-tech/laravel-model-expires

1.1.0 2024-03-27 11:05 UTC

This package is auto-updated.

Last update: 2024-09-08 16:49:58 UTC


README

Laravel 模型过期

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

使用本包,可以为任何模型添加过期日期,并从查询中排除过期模型。在需要时,您可以为即将过期的模型发送通知。您还可以为每个模型设置删除日期,并使用命令自动清理它们。

安装

您可以通过composer安装此包

composer require maize-tech/laravel-model-expires

您可以通过以下命令发布配置和迁移文件并运行迁移:

php artisan model-expires:install

这是已发布的配置文件内容

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' => [
            //
        ],
    ],
];

使用方法

基本使用

要使用此包,请将Maize\ModelExpires\HasExpiration特质添加到所有需要设置过期日期的模型中

<?php

namespace App\Models;

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

class User extends Model
{
    use HasExpiration;
}

就这样!从现在开始,每次您想要设置过期和/或删除日期时,只需调用setExpiresAt方法即可

$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

检查过期和删除剩余天数

您还可以检查模型是否已过期,并计算其过期(或删除)前的天数

$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

排除过期模型

要排除过期模型,您只需使用withoutExpired作用域方法即可

$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

仅检索过期模型

要检索过期模型,您只需使用onlyExpired作用域方法即可

$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

默认过期日期

如果您想定义默认过期日期,可以有两种方式实现。

首先,您可以在config/model-expires.php配置文件中设置expires_after_days属性值。设置后,包括具有Maize\ModelExpires\HasExpiration特质的模型在创建时将自动具有过期日期

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

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

第二种方式是覆盖所有希望具有默认过期日期的模型中的defaultExpiresAt方法

<?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
    }
}

默认删除日期

如果您想定义默认删除日期,可以有两种方式实现。

首先,您可以在config/model-expires.php配置文件中设置deletes_after_days属性值。设置后,包括具有Maize\ModelExpires\HasExpiration特质的模型在创建时将自动具有删除日期

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

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

第二种方式是覆盖所有希望具有默认删除日期的模型中的defaultDeletesAt方法

<?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
    }
}

安排过期检查

此包附带expires:check命令,该命令会自动为所有即将过期的模型触发ModelExpiring事件。

为此,您应该定义您希望多久触发一次事件。您只需覆盖所有使用HasExpiration特质的模型的fireExpiringEventBeforeDays方法即可

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
    }
}

默认情况下,该方法返回一个空数组,这意味着模型永远不会触发过期事件。

完成后,您可以使用控制台内核的schedule方法(通常位于App\Console目录下)每天安排此命令。

use Maize\ModelExpires\Commands\ModelExpiresDeleteCommand;

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

安排模型删除

此包还附带expires:delete命令,该命令会自动删除所有过期和可删除的模型。当使用Laravel的调度来自动化执行时,这非常有用。您只需将以下指令添加到控制台内核的schedule方法(通常位于App\Console目录下)中即可

use Maize\ModelExpires\Commands\ModelExpiresDeleteCommand;

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

测试

composer test

变更日志

请参阅变更日志获取最近更改的更多信息。

贡献

请参阅贡献指南获取详细信息。

安全漏洞

请查阅我们的安全策略了解如何报告安全漏洞。

鸣谢

许可证

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