Created
November 11, 2015 23:47
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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