@param Builder $query
@param $ago
@param null $endDateTime

@return mixed
774153
public function scopeFromMonthsAgo(Builder $query, $ago, $endDateTime = null)
    {
        return $this->scopeFromDateTimeRange(DateTime::getMonthsAgo($ago, $endDateTime));
    }
1326257
public function getRelationCountQuery(Builder $query, Builder $parent)
    {
        return $this->getRelationExistenceQuery($query, $parent, new Expression('count(*)'));
    }
359854
protected function setKeysForSaveQuery(Builder $query)
    {
        $query->where('group_id', $this->group_id)
              ->where('permission', $this->permission);

        return $query;
    }
1067021
public static function getCenturiesAgo($ago, $endDateTime = null)
    {
        return static::getDateTimeRange(
            Carbon::now()->subYears($ago * 100)->startOfCentury(),
            $endDateTime ?: Carbon::now()->subYears($ago * 100)->endOfCentury()
        );
    }
838810
public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        $query->select($columns)->from(
            $query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash()
        );

        $query->getModel()->setTable($hash);

        return $query->whereColumn(
            $hash.'.'.$this->ownerKey, '=', $this->getQualifiedForeignKeyName()
        );
    }
814170
protected function performJoin(Builder $query = null)
    {
        $query = $query ?: $this->query;

        $throughParents = array_reverse($this->throughParents);
        $foreignKeys = array_reverse($this->foreignKeys);
        $localKeys = array_reverse($this->localKeys);

        $segments = explode(' as ', $query->getQuery()->from);

        $alias = $segments[1] ?? null;

        foreach ($throughParents as $i => $throughParent) {
            $predecessor = $throughParents[$i - 1] ?? $this->related;

            $prefix = $i === 0 && $alias ? $alias.'.' : '';

            $this->joinThroughParent($query, $throughParent, $predecessor, $foreignKeys[$i], $localKeys[$i], $prefix);
        }
    }
846557
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        if ($parentQuery->getQuery()->from == $query->getQuery()->from) {
            return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns);
        }

        return $query->select($columns)->whereColumn(
            $this->getQualifiedForeignKeyName(), '=', $query->qualifyColumn($this->ownerKey)
        );
    }
1023285
public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        $query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash());

        $query->getModel()->setTable($hash);

        return $query->select($columns)->whereColumn(
            $this->getQualifiedParentKeyName(), '=', $hash.'.'.$this->getForeignKeyName()
        );
    }
682101
public function getForRange($key, \DateTime $start, \DateTime $end)
    {
        if (!$this->hasForRange($key, $start, $end)) {
            return null;
        }

        return $this->data[$this->keyForRange($key, $start, $end)];
    }
466079
public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*'])
	{
		$query->select($columns);

		$key = $this->wrap($this->getQualifiedParentKeyName());

		return $query->where($this->getHasCompareKey(), '=', new Expression($key));
	}
714308
public function scopeMostVisited(Builder $query, $limit = null)
    {
        $query->select(\DB::raw('nodes.*, count(*) as `aggregate`'))
            ->join('node_site_view', 'nodes.id', '=', 'node_site_view.node_id')
            ->groupBy('nodes.id')
            ->orderBy('aggregate', 'desc');

        if ($limit)
        {
            $query->limit($limit);
        }

        return $query;
    }
1025571
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        $query = parent::getRelationExistenceQuery($query, $parentQuery, $columns);

        if (is_array($this->foreignKeys[0])) {
            $column = $this->throughParent->qualifyColumn($this->foreignKeys[0][0]);

            $query->where($column, '=', $this->farParent->getMorphClass());
        }

        return $query;
    }
589250
protected function sortQuery(Builder $query, array $sort_order_options, Model $temp_instance, array $columns)
	{
		$allowed_directions = [
			'ASC',
			'DESC',
		];

		foreach ($sort_order_options as $order_by => $direction) {
			if (in_array(strtoupper($direction), $allowed_directions)) {
				$split = explode(self::GLUE, $order_by);

				// Sorts deeper than the depth restriction + 1 are not allowed
				// Depth restriction is offset by 1 because sorts terminate with a column
				// i.e. 'users.posts.title' => 'asc' but the depth we expect is 'users.posts'
				if (count($split) > ($this->getDepthRestriction() + 1)) {
					// Unset the disallowed sort
					unset($sort_order_options[$order_by]);
					continue;
				}

				if (in_array($order_by, $columns)) {
					$query->orderBy($order_by, $direction);
				} else {
					// Pull out orderBy field
					$field = array_pop($split);

					// Select only the base table fields, don't select relation data. Desired relation data
					// should be explicitly included
					$base_table = $temp_instance->getTable();
					$query->selectRaw("$base_table.*");

					$this->applyNestedJoins($query, $split, $temp_instance, $field, $direction);
				}
			}
		}
	}
4829
public static function getMonthsAgo($ago, $endDateTime = null)
    {
        return static::getDateTimeRange(
            Carbon::now()->subMonths($ago)->startOfMonth(),
            $endDateTime ?: Carbon::now()->subMonths($ago)->endOfMonth()
        );
    }
949644
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        if ($parentQuery->getQuery()->from == $query->getQuery()->from) {
            return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns);
        }

        $modelTable = $query->getModel()
            ->getTable();

        return $query->select($columns)
            ->whereColumn(
                $this->getQualifiedForeignKey(), '=', is_array($this->ownerKey) ? //Check for multi-columns relationship
                array_map(function ($k) use ($modelTable) {
                    return $modelTable.'.'.$k;
                }, $this->ownerKey) : $modelTable.'.'.$this->ownerKey
            );
    }
150480
function buildQuery(query, builder = new Builder()) {
    for (let i = 0; i < query.length; i++) {
        const method = query[i]

        if (Array.isArray(method)) {
            builder.and(buildQuery(method, new NonCapture()))
            continue
        }

        if (!method instanceof Method) {
            // At this point, there should only be methods left, since all parameters are already taken care of.
            // If that's not the case, something didn't work out.
            throw new SyntaxException(`Unexpected statement: ${method}`)
        }

        const parameters = []
        // If there are parameters, walk through them and apply them if they don't start a new method.
        while (query[i + 1] && !(query[i + 1] instanceof Method)) {
            parameters.push(query[i + 1])

            // Since the parameters will be appended to the method object, they are already parsed and can be
            // removed from further parsing. Don't use unset to keep keys incrementing.
            query.splice(i + 1, 1)
        }

        try {
            // Now, append that method to the builder object.
            method.setParameters(parameters).callMethodOn(builder)
        } catch (e) {
            const lastIndex = parameters.length - 1
            if (Array.isArray(parameters[lastIndex])) {
                if (lastIndex !== 0) {
                    method.setParameters(parameters.slice(0, lastIndex))
                }
                method.callMethodOn(builder)
                builder.and(buildQuery(parameters[lastIndex], new NonCapture()))
            } else {
                throw new SyntaxException(`Invalid parameter given for ${method.origin}`)
            }
        }
    }

    return builder
}
774601
protected function setKeysForSaveQuery(Builder $query)
	{
		$query->where($this->morphType, $this->getAttribute($this->morphType));

		return parent::setKeysForSaveQuery($query);
	}