mirror of
https://github.com/flarum/framework.git
synced 2025-01-19 18:12:59 +08:00
patched up the Scope implementation as requested
This commit is contained in:
parent
4b7eeb2564
commit
a7bcc79085
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user