maize-tech / laravel-model-expires
Laravel 模型过期
Requires
- php: ^8.1
- illuminate/console: ^10.0|^11.0
- illuminate/database: ^10.0|^11.0
- illuminate/notifications: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.14.1
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.8|^8.1
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.3
- spatie/pest-plugin-test-time: ^2.1
README
Laravel 模型过期
使用本包,可以为任何模型添加过期日期,并从查询中排除过期模型。在需要时,您可以为即将过期的模型发送通知。您还可以为每个模型设置删除日期,并使用命令自动清理它们。
安装
您可以通过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)。请参阅许可证文件获取更多信息。