News

Assign Expiration Dates to Eloquent Models

Published
Assign Expiration Dates to Eloquent Models image

Laravel Model Expires is a package by Mark van den Broek that assigns expiration dates to models and gives you some conveniences for working with them. The package provides a convenient trait to make your models “expirable” in a snap:

namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
use Mvdnbrk\EloquentExpirable\Expirable;
 
class Subscription extends Model
{
use Expirable;
}

You’ll also need to set up an expires_at field to the model’s database table, which is easy using the package’s helper in your migrations:

Schema::create('subscriptions', function (Blueprint $table) {
$table->expires();
});

The expires_at column is cast to a Carbon instance, and you can also customize the name of the column if you desire in your model.

You’ll want to refer to the package’s documentation for full details of configuration, setting expiration dates, and working with models. Here are a few examples of methods provided to “expirable” models:

// Already has expired?
$subscription->expired()
 
// Will expire in the future?
$subscription->willExpire()
 
// Only get expired models
$subscriptions = Subscription::onlyExpired()->get();
 
// Models expiring in the future
$subscriptions = Subscription::expiring()->get();

You can learn more about this package, get full installation instructions, and view the source code on GitHub at mvdnbrk/laravel-model-expires.


This package was submitted to our Laravel News Links section. Links is a place the community can post packages and tutorials around the Laravel ecosystem. Follow along on Twitter @LaravelLinks

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Filed in

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article