patched up the Scope implementation as requested

This commit is contained in:
Daniel Klabbers 2017-11-27 14:17:17 +01:00
parent 4b7eeb2564
commit a7bcc79085

View File

@ -17,62 +17,16 @@ use Illuminate\Database\Eloquent\Scope;
class RegisteredTypesScope implements Scope
{
/**
* The index at which we added a where clause.
*
* @var int
*/
protected $whereIndex;
/**
* The index at which we added where bindings.
*
* @var int
*/
protected $bindingIndex;
/**
* The number of where bindings we added.
*
* @var int
*/
protected $bindingCount;
/**
* Apply the scope to a given Eloquent query builder.
*
* @param Builder $builder
* @param Model $post
* @return void
*/
public function apply(Builder $builder, Model $post)
{
$query = $builder->getQuery();
$this->whereIndex = count($query->wheres);
$this->bindingIndex = count($query->getRawBindings()['where']);
$types = array_keys($post::getModels());
$this->bindingCount = count($types);
$query->whereIn('type', $types);
}
/**
* Remove the scope from the given Eloquent query builder.
*
* @param Builder $builder
* @param Model $post
* @return void
*/
public function remove(Builder $builder, Model $post)
{
$query = $builder->getQuery();
unset($query->wheres[$this->whereIndex]);
$query->wheres = array_values($query->wheres);
$whereBindings = $query->getRawBindings()['where'];
array_splice($whereBindings, $this->bindingIndex, $this->bindingCount);
$query->setBindings(array_values($whereBindings));
}
}