mvdnbrk/laravel-model-expires
Composer 安装命令:
composer require mvdnbrk/laravel-model-expires
包简介
A package to assign expiration dates to Eloquent models
README 文档
README
Assign expiration dates to Eloquent models
Installation
You can install the package via composer:
composer require mvdnbrk/laravel-model-expires
Usage
To use an expiration date on a model, use the Expirable trait:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Mvdnbrk\EloquentExpirable\Expirable; class Subscription extends Model { use Expirable; }
You should add an expires_at column to your database table.
This packages contains a helper method to create this column:
class CreateSubscriptionsTable extends Migration { public function up(): void { Schema::create('subscriptions', function (Blueprint $table) { $table->expires(); }); } public function down(): void { Schema::dropExpires(); } }
The Expirable trait will automatically cast the expires_at attribute to a DateTime / Carbon instance for you.
Customizing the column name
You may customize the column name by setting the EXIRES_AT constant or by overriding the getExpiresAtColumn method on your model.
class Subscription extends Model { use Expirable; const EXPIRES_AT = 'ends_at'; }
$table->expires('ends_at'); $table->dropExpires('ends_at');
Setting expiration
You may set the expiration of a model by setting the expires_at attribute with a TTL in seconds:
$subscription->expires_at = 600;
Instead of passing the number of seconds as an integer, you may also pass a DateTime instance representing the expiration date:
$subscription->expires_at = now()->addMinutes(10);
Discarding expiration
You may discard the expiration of a model by setting a negative or zero TTL or use the discardExpiration method:
$subscription->expires_at = 0; $subscription->expires_at = -5; $subscription->discardExpiration()->save();
Determining expiration
To determine if a given model instance has expired, use the expired method:
if ($subscription->expired()) { // }
To determine if a given model will expire in the future use the willExpire method:
if ($subscription->willExpire()) { // }
Querying models
The withoutExpired method will retrieve models that are not expired:
$subscriptions = App\Models\Subscription::withoutExpired()->get();
The onlyExpired method will retrieve only the expired models:
$subscriptions = App\Models\Subscription::onlyExpired()->get();
The expiring method will retrieve only models that will expire in the future:
$subscriptions = App\Models\Subscription::expiring()->get();
The notExpiring method will retrieve only models that will not expire:
$subscriptions = App\Models\Subscription::notExpiring()->get();
Todo
- Add a
expired:pruneconsole command to delete expired models, or perform custom implementation. - Add a query scope that will query models that will expire in
...
Testing
composer test
Changelog
Please see CHANGELOG for more information 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.
mvdnbrk/laravel-model-expires 适用场景与选型建议
mvdnbrk/laravel-model-expires 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.47k 次下载、GitHub Stars 达 166, 最近一次更新时间为 2019 年 11 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「date」 「model」 「laravel」 「expiration」 「eloquent」 「expire」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mvdnbrk/laravel-model-expires 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mvdnbrk/laravel-model-expires 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mvdnbrk/laravel-model-expires 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Tools to convert between .NET and PHP date formats
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Interfaces for web resource models and services to retrieve and create them
A powerful event calendar Tool for Laravel's Nova 5.
Date component is a set of methods to help with the manipulation of dates.
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 17.47k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 166
- 点击次数: 6
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-11-07
