Scope a query to only include models by owner.

@param \Illuminate\Database\Eloquent\Builder $query
@param \Cog\Contracts\Ownership\CanBeOwner $owner
@return \Illuminate\Database\Eloquent\Builder
198717
public function scopeWhereNotOwnedBy(Builder $query, CanBeOwnerContract $owner)
    {
        return $query->where(function (Builder $q) use ($owner) {
            $q->where('owned_by_id', '!=', $owner->getKey())
                ->orWhere('owned_by_type', '!=', $owner->getMorphClass());
        });
    }
637240
public function scopeOrderModel($query)
    {
        $parentFields = $this->getParentFieldName();
        if ($parentFields) {
            $parentFields = (array) $parentFields;
            foreach ($parentFields as $parentFieldName) {
                $query->where($parentFieldName, $this->{$parentFieldName});
            }
        }

        return $query->orderBy($this->getOrderField(), 'ASC');
    }
903879
public function scopeWhereLikedBy(Builder $query, $userId = null)
    {
        return app(LikeableServiceContract::class)
            ->scopeWhereLikedBy($query, LikeType::LIKE, $userId);
    }
207058
public function scopeOrder(Builder $query)
    {
        if ($order = config('typicms.'.$this->getTable().'.order')) {
            foreach ($order as $column => $direction) {
                $query->orderBy($column, $direction);
            }
        }

        return $query;
    }
689643
public function isOwnedBy(CanBeOwnerContract $owner)
    {
        if (!$this->hasOwner()) {
            return false;
        }

        return $owner->getKey() === $this->getOwner()->getKey();
    }
214846
public function scopeRole(Builder $query, $roles = [])
    {
        $query->with('roles')->whereNotNull('tbl_users.id');

        if (!empty($roles)) {
            $query->whereHas('roles', function ($query) use ($roles) {
                $query->whereIn('tbl_roles.name', $roles);
            });
        }

        return $query;
    }
1044319
public function scopeWithTranslations(Builder $query, string $locale = null): Builder
    {
        $locale = $locale ?: $this->getLocale();

        return $query->with(['translations' => function (HasMany $query) use ($locale) {
            $query->where('locale', $locale);
        }]);
    }
55463
protected function setKeysForSaveQuery(Builder $query)
    {
        $query->where($this->foreignKey, $this->getAttribute($this->foreignKey));

        return $query->where($this->otherKey, $this->getAttribute($this->otherKey));
    }
1296242
public static function notAllowed(OwnableContract $ownable, CanBeOwnerContract $owner)
    {
        return new static(sprintf('Model `%s` not allows owner of type `%s`.', get_class($ownable), get_class($owner)));
    }
456463
public function scopeOwnedBy(Builder $query, Model $related, ?string $key = null): Builder
    {
        if (\is_null($key)) {
            $key = $related->getForeignKey();
        }

        return $query->where($key, $related->getKey());
    }
603070
public function withDefaultOwner(CanBeOwnerContract $owner = null)
    {
        $this->defaultOwner = $owner ?: $this->resolveDefaultOwner();
        if (isset($this->withDefaultOwnerOnCreate)) {
            $this->withDefaultOwnerOnCreate = false;
        }

        return $this;
    }
1040234
public function scopeWhereRatedBy(Builder $query, $userId = null)
    {
        return app(RatingServiceContract::class)
            ->scopeWhereRatedBy($query, $userId);
    }
971068
public function scopePending(Builder $query)
    {
        return $query->where('status', Node::PENDING)
            ->where('published_at', '>', Carbon::now());
    }
299384
public function scopeInList(Builder $query)
    {
        return $query->listifyScope()->whereNotNull($this->getTable() . "." . $this->positionColumn());
    }
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;
    }
1226117
protected function setKeysForSaveQuery(Builder $query)
    {
        $query->where($this->morphType, $this->morphClass);

        return parent::setKeysForSaveQuery($query);
    }
181696
public function createReferenceInverseSideQuery(PropertyMetadata $mapping, $owner)
    {
        $targetClass = $this->dm->getClassMetadata($mapping->targetDocument);
        $mappedBy    = $targetClass->getPropertyMetadata($mapping->mappedBy);
        $qb          = $this->dm->createQueryBuilder($mapping->targetDocument);

        //add the owning document's identifier as criteria for the mapped reference
        $qb->attr($mappedBy->name)->equals($owner);

        if ($mapping->mappedIndex !== null) {
            $qb->index($mapping->mappedIndex);

            //if mappedIndex is not set, requireIndexes is in effect and the
            //querybuilder cannot find a usable index then the query will throw
            //an exception on execution
        }

        if ($mapping->requireIndexes !== null) {
            //if configured this will override any setting on the dm or class
            $qb->requireIndexes($mapping->requireIndexes);
        }

        $qb->addCriteria((array)$mapping->criteria);

        foreach ((array)$mapping->prime as $attribute) {
            $qb->attr($attribute)->prime(true);
        }

        $query = $qb->getQuery();

        $query->reverse($mapping->reverse);
        $query->limit($mapping->association === PropertyMetadata::ASSOCIATION_TYPE_REFERENCE_ONE ? 1 : $mapping->limit);

        return $query;
    }