@param \Illuminate\Database\Eloquent\Builder $query
@param array|\ArrayAccess|\Spatie\Tags\Tag $tags

@return \Illuminate\Database\Eloquent\Builder
434893
public function scopeWithAllTags(Builder $query, $tags, string $type = null): Builder
    {
        $tags = static::convertToTags($tags, $type);

        collect($tags)->each(function ($tag) use ($query) {
            $query->whereIn("{$this->getTable()}.{$this->getKeyName()}", function ($query) use ($tag) {
                $query->from('taggables')
                    ->select('taggables.taggable_id')
                    ->where('taggables.tag_id', $tag ? $tag->id : 0);
            });
        });

        return $query;
    }
1021650
public function selectByTags(array $tags)
    {
        $result = [];
        foreach ($this->array['Key2Tags'] as $key => $keyTags) {
            $found = true;
            foreach ($tags as $t) {
                if (array_search($t, $keyTags)===false) {
                    $found = false;
                }
            }

            if ($found) {
                $result[] = $key;
            }
        }

        return $result;
    }
795052
public function applyToQuery(Builder $query)
    {
        $this->query = $query;

        foreach ($this->request->all() as $filter => $value) {
            $method = Str::camel($filter);

            if ($this->filterCanBeApplied($method, $value)) {
                call_user_func([$this, $method], $value);
            }
        }

        return $query;
    }
151132
public function buildQuery(Builder $query = null): Builder
    {
        $query = $this->getSubQuery($this->meta, $query, $this->conditions['rules'], $this->conditions['condition']);

        if ($this->offset > 0) {
            $query->offset($this->offset);
        }
        if ($this->limit > 0) {
            $query->limit($this->limit);
        }

        return $query;
    }
236683
public function queryFromFilter(Query $query, array $data = [])
    {
        $query = parent::queryFromFilter($query, $data);

        //"Tag" field
        if (!empty($data['tag']) && strlen($data['tag']) > 2) {
            $query->matching('Tags', function (Query $q) use ($data) {
                return $q->where([sprintf('%s.tag', $this->Tags->getAlias()) => $data['tag']]);
            })->distinct();
        }

        return $query;
    }
1146840
public function newEloquentBuilder($query)
    {
        return \tap(new Builder($query), function ($builder) {
            $builder->setTenantor($this->tenantor ?? null);
        });
    }
927165
public function addFilter($name, $query = array(), $tag = 'root_type')
	{
		if (is_array($query))
		{
			$string = $this->makeQueryString($query);
		}
		elseif (is_string($query))
		{
			$string = $query;
			if ($name == 'BoundingBox')
			{
				$this->query->setOptions(array('geo'=> true));
			}
		}
		$filterParams = array();
		$filterParams['key'] = $name;
		$filterParams['query'] = $string;
		if ($tag)
		{
			$filterParams['tag'] = $tag;
		}

		$this->query->createFilterQuery($filterParams);
		return $this;
	}
309366
protected function replayMacros(Builder $query)
    {
        foreach ($this->macroBuffer as $macro) {
            $query->{$macro['method']}(...$macro['parameters']);
        }

        return $query;
    }
1064874
protected function searchQuery(Builder $query, string $phrase)
    {
        $r = HCPages::getTableName();
        $t = HCPagesTranslations::getTableName();

        return $query->join($t, "$r.id", "=", "$t.record_id")
            ->orWhere("$t.title", 'LIKE', '%' . $phrase . '%')
            ->orWhere("$t.slug", 'LIKE', '%' . $phrase . '%')
            ->orWhere("$t.content", 'LIKE', '%' . $phrase . '%');
    }
678631
public function scopeSuggestionsFor($query, string $type, array $tags)
    {
        return (new SuggestionQuery($query))->type($type)->apply($tags);
    }
757334
public function addTag(Tag $tag)
    {
        $tag->setBlog($this);
        $this->tags->add($tag);

        return $tag;
    }
714848
public function operatorterm(Query $query, TokenInterface $token)
    {
        $terms = explode(',', strtolower($token->value()));
        $conjunction = $token->negated() ? 'NOT IN' : 'IN';

        if (empty($terms)) {
            return $query;
        }

        $conditions = [
            "Contents.id {$conjunction}" => TableRegistry::get('Taxonomy.EntitiesTerms')
                ->find()
                ->select(['EntitiesTerms.entity_id'])
                ->where(['EntitiesTerms.table_alias' => $this->alias()])
                ->matching('Terms', function ($q) use ($terms) {
                    return $q->where(['Terms.slug IN' => $terms]);
                })
        ];

        if (!empty($conditions)) {
            if ($token->where() === 'or') {
                $query->orWhere($conditions);
            } elseif ($token->where() === 'and') {
                $query->andWhere($conditions);
            } else {
                $query->where($conditions);
            }
        }

        return $query;
    }
825572
protected function setKeysForSaveQuery(Builder $query)
    {
        if (isset($this->attributes[$this->getKeyName()])) {
            return parent::setKeysForSaveQuery($query);
        }

        $query->where($this->foreignKey, $this->getOriginal(
            $this->foreignKey, $this->getAttribute($this->foreignKey)
        ));

        return $query->where($this->relatedKey, $this->getOriginal(
            $this->relatedKey, $this->getAttribute($this->relatedKey)
        ));
    }
887594
protected function makeJoins(Builder $query)
    {
        foreach ($this->getJoins() as $table => $keys) {
            $query->leftJoin($table, function ($join) use ($keys) {
                $join->on($keys[0], '=', $keys[1]);
                if (array_key_exists(2, $keys) && array_key_exists(3, $keys)) {
                    $join->whereRaw($keys[2] . ' = "' . $keys[3] . '"');
                }
            });
        }
    }
925640
protected function buildSelectQueryBody($query)
    {
        $distinctSql = $query->getDistinct() ? ' DISTINCT' : '';
        $whatSql = $this->buildSelectWhatPart($query);
        $fromSql = $this->buildSelectFromPart($query);
        $whereSql = $this->buildWherePart($query);
        $groupBySql = $this->buildSelectGroupByPart($query);
        $havingSql = $this->buildSelectHavingPart($query);
        $joinSql = $this->buildSelectJoinPart($query);

        $result = 'SELECT' . $distinctSql . ' ' . $whatSql
            . $fromSql
            . $joinSql
            . $whereSql
            . $groupBySql
            . $havingSql;

        return $result;
    }
395212
protected function select(string $query, array $parameters = [], string $allowedTags = ""): array
    {
        if (!is_null($this->pager)) {
            $query .= $this->pager->getSqlLimit();
        }
        $statement = $this->query($query, $parameters);
        $statement->setAllowedHtmlTags($allowedTags);
        $results = [];
        while ($row = $statement->next($this->fetchStyle)) {
            $results[] = $row;
        }
        return $results;
    }
250823
public function addTag(\Amulen\ClassificationBundle\Entity\Tag $tags)
    {
        $this->tags[] = $tags;

        return $this;
    }