All Projects → mvdnbrk → Laravel Model Expires

mvdnbrk / Laravel Model Expires

Licence: mit
A package to assign expiration dates to Eloquent models.

Projects that are alternatives of or similar to Laravel Model Expires

Laravel Likeable
Rate Eloquent models with Likes and Dislikes in Laravel. Development moved to Laravel Love package!
Stars: ✭ 95 (-35.81%)
Mutual labels:  eloquent, laravel
Laravel Cacheable
Rinvex Cacheable is a granular, intuitive, and fluent caching system for eloquent models. Simple, but yet powerful, plug-n-play with no hassle.
Stars: ✭ 107 (-27.7%)
Mutual labels:  eloquent, laravel
Sarala
Javascript library to communicate with RESTful API built following JSON API specification. inspired by Laravel’s Eloquent
Stars: ✭ 101 (-31.76%)
Mutual labels:  eloquent, laravel
Flattable
It helps you manage de-normalized tables
Stars: ✭ 81 (-45.27%)
Mutual labels:  eloquent, laravel
Sieve
A simple, clean and elegant way to filter Eloquent models.
Stars: ✭ 123 (-16.89%)
Mutual labels:  eloquent, laravel
Laravel Nullable Fields
Handles saving empty fields as null for Eloquent models
Stars: ✭ 88 (-40.54%)
Mutual labels:  eloquent, laravel
Laravel Settings
Store key value pair in database as settings
Stars: ✭ 107 (-27.7%)
Mutual labels:  eloquent, laravel
Laravel Schedulable
Schedule and unschedule eloquent models elegantly without cron jobs
Stars: ✭ 78 (-47.3%)
Mutual labels:  eloquent, laravel
Guardian
Eloquent Guardian is a simple permissions system for your users. While there are many other packages for permissions, this one solves everything in the most eloquent way.
Stars: ✭ 121 (-18.24%)
Mutual labels:  eloquent, laravel
Laravel Invoicable
Easy invoice creation for Laravel
Stars: ✭ 118 (-20.27%)
Mutual labels:  eloquent, laravel
Laravel Approvable
Easily add an approval process to any laravel model.
Stars: ✭ 79 (-46.62%)
Mutual labels:  eloquent, laravel
Eager Load Pivot Relations
Eager load pivot relations for Laravel Eloquent's BelongsToMany relation.
Stars: ✭ 134 (-9.46%)
Mutual labels:  eloquent, laravel
Eloquent Approval
Approval process for Laravel Eloquent models
Stars: ✭ 79 (-46.62%)
Mutual labels:  eloquent, laravel
Laravel Prefixed Ids
Friendly prefixed IDs for Laravel models
Stars: ✭ 88 (-40.54%)
Mutual labels:  eloquent, laravel
Requent
A GraphQL like interface to map a request to eloquent query with data transformation for Laravel.
Stars: ✭ 78 (-47.3%)
Mutual labels:  eloquent, laravel
Laravel Translatable
Making Eloquent models translatable
Stars: ✭ 1,390 (+839.19%)
Mutual labels:  eloquent, laravel
Laravel Ownership
Laravel Ownership simplify management of Eloquent model's owner.
Stars: ✭ 71 (-52.03%)
Mutual labels:  eloquent, laravel
Laravel Lucene Search
Laravel 4.2, 5.* package for full-text search over Eloquent models based on ZF2 Lucene.
Stars: ✭ 75 (-49.32%)
Mutual labels:  eloquent, laravel
Searchable
Search/filter functionality for Laravel's Eloquent models
Stars: ✭ 113 (-23.65%)
Mutual labels:  eloquent, laravel
Querybuilderparser
A simple to use query builder for the jQuery QueryBuilder plugin for use with Laravel.
Stars: ✭ 126 (-14.86%)
Mutual labels:  eloquent, laravel
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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].