Skip to content

Instantly share code, notes, and snippets.

@Keoghan
Created November 11, 2015 23:47
Show Gist options
  • Save Keoghan/03ecbdd7920b013896fa to your computer and use it in GitHub Desktop.
Save Keoghan/03ecbdd7920b013896fa to your computer and use it in GitHub Desktop.
A simple Expires trait for use in a laravel app on an Eloquent model. Requires ExpiringScope
<?php
namespace App;
use Carbon\Carbon;
trait Expires
{
public static function bootExpires()
{
static::addGlobalScope(new ExpiringScope);
static::saving(function($model) {
$model->expires_at = Carbon::now()->addMinutes(15);
});
}
public function willExpire()
{
return ! is_null($this->expires_at)
&& $this->expires_at->gt(Carbon::now());
}
public function hasExpired()
{
return ! is_null($this->expires_at)
&& $this->expires_at->format('Y') > 0
&& $this->expires_at->lt(Carbon::now());
}
/**
* Get the name of the "expires at" column.
*
* @return string
*/
public function getExpiresAtColumn()
{
return 'expires_at';
}
/**
* Get the fully qualified "expires at" column.
*
* @return string
*/
public function getQualifiedExpiresAtColumn()
{
return $this->getTable().'.'.$this->getExpiresAtColumn();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment