Assign Expiration Dates to Eloquent Models


October 1st, 2020


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:

1namespace App\Models;
3use Illuminate\Database\Eloquent\Model;
4use Mvdnbrk\EloquentExpirable\Expirable;
6class Subscription extends Model
8 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:

1Schema::create('subscriptions', function (Blueprint $table) {
2 $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:

1// Already has expired?
4// Will expire in the future?
7// Only get expired models
8$subscriptions = Subscription::onlyExpired()->get();
10// Models expiring in the future
11$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

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.