Skip to content

Instantly share code, notes, and snippets.

@vanchelo
Created August 14, 2014 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanchelo/556318e6d3b5ecbd0fff to your computer and use it in GitHub Desktop.
Save vanchelo/556318e6d3b5ecbd0fff to your computer and use it in GitHub Desktop.
ActiveScope with Trait
<?php
class ActiveScope implements ScopeInterface
{
/**
* Apply the scope to a given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
public function apply(Builder $builder)
{
$builder->whereActive(1);
}
/**
* Remove the scope from the given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
public function remove(Builder $builder)
{
$query = $builder->getQuery();
unset($query->wheres['active']);
}
}
trait ActiveTrait
{
public function bootActiveTrait()
{
static::addGlobalScope(new ActiveScope);
}
}
class Post extends Eloquent
{
use ActiveTrait;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment