New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

mvdnbrk/laravel-model-expires

A package to assign expiration dates to Eloquent models
17,878 166
Install
composer require mvdnbrk/laravel-model-expires
Latest Version:v1.8.1
PHP:^7.2 || ^8.0
Laravel:6 · 7 · 8
Older releases:5
License:MIT
Last Updated:Sep 7, 2022
Links: GitHub  ·  Packagist
Maintainer: mvdnbrk
Laravel Model Expires

PHP version Latest Version on Packagist Software License Tests Code style Total Downloads

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:prune console 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.

Related Packages

createnl/expires

Set expiration date for database records, like future deleted at columns

3,761 2
jenssegers/model

An Laravel eloquent-like model class, for Laravel and other frameworks

4,754,764 394
cerbero/eloquent-inspector

Inspect Laravel Eloquent models to collect properties, relationships and more.

55,512 112
czim/laravel-nestedupdater

Eloquent model updater for nested relations data for Laravel.

54,240 19
supportpal/eloquent-model

An Laravel eloquent-like model class, for Laravel and other frameworks

10,673 0

Version History

Version Released PHP Laravel License
v1.8.1 ^7.2 || ^8.0 ^6.0 || ^7.0 || ^8.0 MIT
v1.8.0 ^7.2 ^6.0 || ^7.0 || ^8.0 MIT
v1.7.0 ^7.1 ^5.5 || ^6.0 || ^7.0 || ^8.0 MIT
v1.6.0 ^7.2 ^5.8 || ^6.0 || ^7.0 || ^8.0 MIT
v1.5.1 ^7.2 ^5.8 || ^6.0 || ^7.0 MIT