Download the PHP package yarob/laravel-expirable without Composer
On this page you can find all versions of the php package yarob/laravel-expirable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download yarob/laravel-expirable
More information about yarob/laravel-expirable
Files in yarob/laravel-expirable
Package laravel-expirable
Short Description Laravel 5.0+ package to add expirable trait to eloquent models
License MIT
Informations about the package laravel-expirable
Laravel Expirable
Add expiring feature to Eloquent models in Laravel 5.
Background
This has been developed to simplify adding expirable
feature to any eloquent model on your laravel project.
Installation
To install the package via Composer:
Then, update config/app.php
by adding an entry for the service provider.
Finally, via terminal, publish the default configuration file (if you need to, see below):
Updating your Eloquent Models
Your models can now use the Expirable
trait.
You must also add expire_at
to your $dates
array in the model as shown in the example below
Migration
Your model MUST have column named expire_at
in the database to store the expiry date value.
You can add this manually via a migration on the intended model $table->timestamp('expire_at')->nullable();
.
Usage
There are three basic functions in Expirable
trait:
hasExpired()
to check if model has expired or not returnsboolean
timeToLive()
returns number of seconds left on models life, could be minus if has expired already! returnsfalse
if not applicable.reviveExpired($numberOfSeconds=null)
if model has expired you can then revive it by using this function and supplying$numberOfSeconds
revival period starting fromnow()
. If$numberOfSeconds
is not supplied then will default back to the value set inexpirable.php
in config folder, if set! Otherwise it will not do anything and returnsfalse
.-
Model has
null
expiry date means a non-expirable model and lives forever, e.g.$user->expire_at = null;
Example:-
Querying Expirable Models
Expired models (passed it’s expiry date) will automatically be excluded from query results. For example:-
will only gets models that has NOT expired or has null
expiry date. Any expired models WILL be auto-excluded.
Including expired Models
You can force expired models to appear in query results using withHasExpiry
Retrieving Only Expired Models
The onlyHasExpiry
method can be used on a relationship query:
This will exclude ONLY null
expiry date from results.
Please note this will get ALL models that have non null
expiry date, regradless of their expiry date.
Retrieving Models has null
expiry
The withoutHasExpiry
method can be used in this case:
This will bring models that has null
expiry date.
Configuration
Configuration is not usually needed, unless you want to set a default revival time to a model(s). A default value of 86400 seconds
is set for user model, as an example, but feel free to change that to any value you want.
Here is an example configuration:
Pay attention that Model name in expirable.php
is case sensitive! so if you have a foo
Model, then
Copyright and License
laravel-expirable was written by Yarob Al-Taay and is released under the MIT License.
Copyright (c) 2017 Yarob Al-Taay