Skip to content

Instantly share code, notes, and snippets.

@willishq
Last active December 29, 2015 15:58
Show Gist options
  • Save willishq/7693878 to your computer and use it in GitHub Desktop.
Save willishq/7693878 to your computer and use it in GitHub Desktop.
Adding active timestamps to Eloquent
<?php
namespace Extreloquent;
use Illuminate\Database\Eloquent\Builder;
/**
* Extension of the Eloquent model to add active dates to data
*
* Class Model
* @package Extreloquent
*/
abstract class Model extends \Illuminate\Database\Eloquent\Model {
/**
* Whether the model has active / inactive dates
*
* @var bool
*/
protected $activeDates = false;
/**
* The name of the "active from" column on the table
*
* @const string
*/
const ACTIVE_FROM = 'active_from';
/**
* The name of the "active_to" column on the tablr
*
* @const string
*/
const ACTIVE_TO = 'active_to';
/**
* Get a new query builder for the model's table.
*
* @param bool $excludeDeleted
* @param bool $excludeInactive
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function newQuery($excludeDeleted = true, $excludeInactive = true)
{
/** @var \Illuminate\Database\Eloquent\Builder|static $builder*/
$builder = parent::newQuery($excludeDeleted);
if ($excludeInactive and $this->activeDates)
{
$builder = $builder->whereRaw(('CURDATE() BETWEEN DATE(' . $this->getQualifiedActiveFromColumn() . ') AND DATE(' . $this->getQualifiedActiveToColumn() . ')'));
}
return $builder;
}
/**
* Get the fully qualified "active from" column
*
* @return string
*/
public function getQualifiedActiveFromColumn()
{
// it's going in a raw query
return '`' . $this->getTable() . '`.`' .$this->getActiveFromColumn() . '`';
}
/**
* Get the fully qualified "active to" column
*
* @return string
*/
public function getQualifiedActiveToColumn()
{
// it's going in a raw query
return '`' . $this->getTable() . '`.`' .$this->getActiveToColumn() . '`';
}
/**
* get the name of the "active from" column
*
* @return string
*/
public function getActiveFromColumn()
{
return static::ACTIVE_FROM;
}
/**
* get the name of the "active to" column
*
* @return string
*/
public function getActiveToColumn()
{
return static::ACTIVE_TO;
}
/**
* Set the "active dates" property on the model.
*
* @param bool $enabled
* @return void
*/
public function setActiveDates($enabled)
{
$this->activeDates = $enabled;
}
/**
* get a new query builder which includes inactive results
*
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public static function withInactive()
{
return with(new static)->newQueryWithInactive();
}
/**
* get a new query builder which includes inactive results
*
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function newQueryWithInactive()
{
return $this->newQuery(true, false);
}
/**
* get a new query builder which includes inactive and soft-deleted results
*
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public static function withTrashedAndInactive()
{
return with(new static)->newQueryWithDeletedAndInactive();
}
/**
* get a new query builder which includes inactive and soft-deleted results
*
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function newQueryWithDeletedAndInactive()
{
return $this->newQuery(false, false);
}
/**
* Get a new query builder that only includes inactive results.
*
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public static function onlyInactive()
{
$instance = new static;
return $instance->whereRaw('CURDATE() NOT BETWEEN DATE(' . $instance->getQualifiedActiveFromColumn() . ') AND DATE(' . $instance->getQualifiedActiveToColumn() . ')');
}
/**
* Get a new query builder that only includes inactive and deleted entries.
*
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public static function onlyTrashedAndInactive() {
$instance = new static;
return $instance->onlyTrashed()->whereRaw('CURDATE() NOT BETWEEN DATE(' . $instance->getQualifiedActiveFromColumn() . ') AND DATE(' . $instance->getQualifiedActiveToColumn() . ')');
}
/**
* Get a new query builder that only includes inactive or deleted entries.
*
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public static function onlyTrashedOrInactive() {
$instance = new static;
return $instance->onlyTrashed()->orWhereRaw('CURDATE() NOT BETWEEN DATE(' . $instance->getQualifiedActiveFromColumn() . ') AND DATE(' . $instance->getQualifiedActiveToColumn() . ')');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment