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

@return mixed
495097
public function scopeFromWeeksAgo(Builder $query, $ago, $endDateTime = null)
    {
        return $this->scopeFromDateTimeRange(DateTime::getWeeksAgo($ago, $endDateTime));
    }
359854
protected function setKeysForSaveQuery(Builder $query)
    {
        $query->where('group_id', $this->group_id)
              ->where('permission', $this->permission);

        return $query;
    }
692963
protected function addExpirationCondition(
        QueryBuilder $query,
        int $expirationInSeconds,
        string $createdAtColumn
    ): QueryBuilder {
        $earliestExpired = $this->ignoreException(function () use ($expirationInSeconds) : DateTimeImmutable {
            return (new DateTimeImmutable())->sub(new DateInterval("PT{$expirationInSeconds}S"));
        });

        $query->andWhere($createdAtColumn . '>' . $this->createTypedParameter($query, $earliestExpired));

        return $query;
    }
398739
public function scopeAdministrators(Builder $query)
    {
        $query->with('roles')->whereNotNull('tbl_users.id')->whereHas('roles', function ($query) {
            $query->whereNotIn('tbl_roles.name', ['member', 'guest', 'client']);
        });


        return $query;
    }
16632
protected function setKeysForSaveQuery(Builder $query)
    {
        $query->where($this->getKeyName(), '=', $this->getKeyForSaveQuery());

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

        $query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $hash.'.'.$this->secondKey);

        if ($this->throughParentSoftDeletes()) {
            $query->whereNull($this->throughParent->getQualifiedDeletedAtColumn());
        }

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

        return $query->select($columns)->whereColumn(
            $parentQuery->getQuery()->from.'.'.$this->localKey, '=', $this->getQualifiedFirstKeyName()
        );
    }
1313429
protected function setKeysForSaveQuery(Builder $query)
    {
        $query->where('discussion_id', $this->discussion_id)
              ->where('user_id', $this->user_id);

        return $query;
    }
995613
protected function getSubQuery(
        Contracts\QueryEntity $meta,
        Builder $query = null,
        array $conditions = [],
        $boolean = 'and'
    ): Builder
    {
        $model = $meta->getModel();
        $query = $query ?? $model->newQuery();

        foreach ($conditions as $condition) {
            $this->buildCondition($meta, $query, $condition, $boolean);
        }
        return $query;
    }
1139201
public function scopeFromYearsAgo(Builder $query, $ago, $endDateTime = null)
    {
        return $this->scopeFromDateTimeRange(DateTime::getYearsAgo($ago, $endDateTime));
    }
1347367
protected function compileFrom(Builder $query, array $from)
    {
        $method = $from['method'];
        $path = $from['path'];

        $hasNamedParameters = $this->hasNamedParameters($path);

        if ($hasNamedParameters) {
            // When there are no wheres provided we will provide an empty array
            // to the replace method. The replace method is able to provide a more
            // specific error message when a named parameter is missing.
            $wheres = $query->wheres ?: [];

            $path = $this->replaceNamedParameters($path, $wheres);
        }

        return compact('method', 'path');
    }
1103496
public function getParticipationsByRangeDate($startDate = '', $endDate = '')
    {
        $em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default');

        $startDateTime = \DateTime::createFromFormat('d/m/Y', $startDate);
        $endDateTime = \DateTime::createFromFormat('d/m/Y', $endDate);

        if ($startDate != '' && $endDate != '') {
            $dateRange = "(e.created_at >='" . $startDateTime->format('Y-m-d') . " 0:0:0' AND e.created_at <= '" . $endDateTime->format('Y-m-d') . " 0:0:0')";
        } elseif ($startDate == '' && $endDate != '') {
            $dateRange = "e.created_at <= '" . $endDateTime->format('Y-m-d') . " 0:0:0'";
        } elseif ($startDate != '' && $endDate == '') {
            $dateRange = "e.created_at >='" . $startDateTime->format('Y-m-d') . " 0:0:0'";
        } else {
            $dateRange = "e.created_at IS NOT NULL";
        }

        $query = $em->createQuery('
            SELECT COUNT(e.id) FROM PlaygroundGame\Entity\Entry e
			WHERE ' . $dateRange . '
            AND e.active=0
		');
        $count = $query->getSingleScalarResult();
        return $count;
    }
668562
public function getEvents(\DateTime $begin, \DateTime $end, array $options = array())
    {
        return $this->getEventsQuery($begin, $end, $options)->getResult();
    }
1325646
public function getRelationExistenceQueryForSelfJoin(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        $query->select($columns);

        $query->from($this->related->getTable().' as '.$hash = $this->getRelationCountHash());

        $this->related->setTable($hash);

        $this->performJoin($query);

        return parent::getRelationExistenceQuery($query, $parentQuery, $columns);
    }
1010066
public function scopeFromCenturiesAgo(Builder $query, $ago, $endDateTime = null)
    {
        return $this->scopeFromDateTimeRange(DateTime::getCenturiesAgo($ago, $endDateTime));
    }
855621
public static function getYearsAgo($ago, $endDateTime = null)
    {
        return static::getDateTimeRange(
            Carbon::now()->subYears($ago)->startOfYear(),
            $endDateTime ?: Carbon::now()->subYears($ago)->endOfYear()
        );
    }
161355
public function scopeForDateTime($query, Carbon $datetime)
    {
        return $query->where('start_at', '<=', $datetime->toDateTimeString())
                        ->where('finish_at', '>=', $datetime->toDateTimeString());
    }
937315
public function getEventsQueryBuilder(\DateTime $begin, \DateTime $end, array $options = array())
    {
        $begin = sprintf("'%s'", $begin->format('Y-m-d H:i:s'));
        $end   = sprintf("'%s'", $end->format('Y-m-d H:i:s'));
        $qb    = $this->createQueryBuilderForGetEvent($options);

        return $qb
            ->andWhere(
                $qb->expr()->orX(
                // Period in event
                    $qb->expr()->andX(
                        $qb->expr()->lte($this->getBeginFieldName(), $begin),
                        $qb->expr()->gte($this->getEndFieldName(), $end)
                    ),
                    // Event in period
                    $qb->expr()->andX(
                        $qb->expr()->gte($this->getBeginFieldName(), $begin),
                        $qb->expr()->lt($this->getEndFieldName(), $end)
                    ),
                    // Event begins during period
                    $qb->expr()->andX(
                        $qb->expr()->lt($this->getBeginFieldName(), $end),
                        $qb->expr()->gte($this->getBeginFieldName(), $begin)
                    ),
                    // Event ends during period
                    $qb->expr()->andX(
                        $qb->expr()->gte($this->getEndFieldName(), $begin),
                        $qb->expr()->lt($this->getEndFieldName(), $end)
                    )
                )
            )
            ;
    }