mirror of
https://github.com/flarum/core.git
synced 2025-07-20 00:01:17 +02:00
patched up the Scope implementation as requested
This commit is contained in:
@@ -17,62 +17,16 @@ use Illuminate\Database\Eloquent\Scope;
|
|||||||
|
|
||||||
class RegisteredTypesScope implements 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.
|
* Apply the scope to a given Eloquent query builder.
|
||||||
*
|
*
|
||||||
* @param Builder $builder
|
* @param Builder $builder
|
||||||
* @param Model $post
|
* @param Model $post
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
public function apply(Builder $builder, Model $post)
|
public function apply(Builder $builder, Model $post)
|
||||||
{
|
{
|
||||||
$query = $builder->getQuery();
|
$query = $builder->getQuery();
|
||||||
|
|
||||||
$this->whereIndex = count($query->wheres);
|
|
||||||
$this->bindingIndex = count($query->getRawBindings()['where']);
|
|
||||||
|
|
||||||
$types = array_keys($post::getModels());
|
$types = array_keys($post::getModels());
|
||||||
$this->bindingCount = count($types);
|
|
||||||
$query->whereIn('type', $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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user