Created
August 14, 2014 13:39
-
-
Save vanchelo/556318e6d3b5ecbd0fff to your computer and use it in GitHub Desktop.
ActiveScope with Trait
This file contains 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 | |
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