A package to assign expiration dates to Eloquent models.
You can install the package via composer:
composer require mvdnbrk/laravel-model-expires
To use an expiration date on a model, use the
Expirabletrait:
use Illuminate\Database\Eloquent\Model; use Mvdnbrk\EloquentExpirable\Expirable;class Subscription extends Model { use Expirable; }
You should add an
expires_atcolumn to your database table.
class CreateSubscriptionsTable extends Migration { public function up(): void { Schema::create('subscriptions', function (Blueprint $table) { $table->expires(); }); }public function down(): void { Schema::dropExpires(); }
}
The
Expirabletrait will automatically cast the
expires_atattribute to a
DateTime/
Carboninstance for you.
You may customize the column name by setting the
EXIRES_ATconstant or by overriding the
getExpiresAtColumnmethod on your model.
class Subscription extends Model { use Expirable;const EXPIRES_AT = 'ends_at';
}
$table->expires('ends_at'); $table->dropExpires('ends_at);
You may set the expiration of a model by setting the
expires_atattribute with a TTL in seconds:
$subscription->expires_at = 600;
Instead of passing the number of seconds as an integer, you may also pass a
DateTimeinstance representing the expiration date:
$subscription->expires_at = now()->addMinutes(10);
You may discard the expiration of a model by setting a negative or zero TTL or use the
discardExpirationmethod:
$subscription->expires_at = 0; $subscription->expires_at = -5;$subscription->discardExpiration()->save();
To determine if a given model instance has expired, use the
expiredmethod:
if ($subscription->expired()) { // }
To determine if a given model will expire in the future use the
willExpiremethod:
if ($subscription->willExpire()) { // }
The
withoutExpiredmethod will retrieve models that are not expired:
$subscriptions = App\Models\Subscription::withoutExpired()->get();
The
onlyExpiredmethod will retrieve only the expired models:
$subscriptions = App\Models\Subscription::onlyExpired()->get();
The
expiringmethod will retrieve only models that will expire in the future:
$subscriptions = App\Models\Subscription::expiring()->get();
The
notExpiringmethod will retrieve only models that will not expire:
$subscriptions = App\Models\Subscription::notExpiring()->get();
expired:pruneconsole command to delete expired models, or perform custom implementation.
...
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.