Returns relation instance for translations.

@param Builder $query
@return \Illuminate\Database\Eloquent\Relations\HasMany
171934
protected function getTranslationsRelation($query)
    {
        /** @var Builder $query */
        /** @var Model|Translatable $model */
        $model = $query->getModel();

        if ( ! method_exists($model, 'translations')) {
            // @codeCoverageIgnoreStart
            throw new RuntimeException('Model ' . get_class($model) . ' is not a translated model.');
            // @codeCoverageIgnoreEnd
        }

        return $model->translations();
    }
758751
public function getRelationCountQueryForSelfJoin(Builder $query, Builder $parent)
	{
		$query->select(new \Fly\Database\Query\Expression('count(*)'));

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

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

		return $query->where($hash.'.'.$this->foreignKey, '=', new \Fly\Database\Query\Expression($key));
	}
760826
protected function setKeysForSaveQuery(Builder $query)
    {
        parent::setKeysForSaveQuery($query);
        $query->where('locale', '=', $this->locale);

        return $query;
    }
248967
protected function useWithTrashed(Builder $query)
    {
        if ($this->withTrashed && $query->getMacro('withTrashed') !== null) {
            return $query->withTrashed();
        }

        return $query;
    }
929709
protected function search(Builder $query, Request $request): Builder
    {
        $phrase = $request->input('q');

        if (!$phrase || strlen($phrase) < $this->minimumSearchInputLength) {
            return $query;
        }

        if ($this->translationModel()) {
            return $this->searchQueryTranslations($query, $phrase);
        }

        return $this->searchQuery($query, $phrase);
    }
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());
    }
1226117
protected function setKeysForSaveQuery(Builder $query)
    {
        $query->where($this->morphType, $this->morphClass);

        return parent::setKeysForSaveQuery($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()
        );
    }
8002
public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*'])
    {
        $query = parent::getRelationQuery($query, $parent, $columns);

        return $query->where($this->morphType, $this->morphClass);
    }
1298960
protected function mappedQuery(Builder $query, $method, ArgumentBag $args)
    {
        $mapping = $this->getMappingForAttribute($args->get('column'));

        if ($this->relationMapping($mapping)) {
            return $this->mappedRelationQuery($query, $method, $args, $mapping);
        }

        $args->set('column', $mapping);

        return $query->callParent($method, $args->all());
    }
1093848
public function queryJoinHelper(BuilderInterface $query)
    {
        $config = $this->getDI()->get('config');
        $modelNameSpace = $config['namespaces']['models'];

        $columns = [];
        // join all active hasOne and belongTo instead of just the parent hasOne
        foreach ($this->entity->activeRelations as $relation) {

            // be sure to skip any relationships that are marked for custom processing
            $relationOptions = $relation->getOptions();
            if (isset($relationOptions) && (array_key_exists('customProcessing',
                        $relationOptions) && ($relationOptions['customProcessing'] === true))
            ) {
                continue;
            }

            // refer to alias or model path to prefix each relationship
            // prefer alias over model path in case of collisions
            $alias = $relation->getAlias();
            $referencedModel = $relation->getReferencedModel();
            if (!$alias) {
                $alias = $referencedModel;
            }

            $type = $relation->getType();
            switch ($type) {
                // structure to always join in belongsTo just in case the query filters by a related field
                case Relation::BELONGS_TO:
                    // create both sides of the join
                    $left = "[$alias]." . $relation->getReferencedFields();
                    $right = $modelNameSpace . $this->model->getModelName() . '.' . $relation->getFields();
                    // create and alias join
                    $query->leftJoin($referencedModel, "$left = $right", $alias);
                    break;

                case Relation::HAS_ONE:
                    // create both sides of the join
                    $left = "[$alias]." . $relation->getReferencedFields();
                    $right = $modelNameSpace . $this->model->getModelName() . '.' . $relation->getFields();
                    // create and alias join
                    $query->leftJoin($referencedModel, "$left = $right", $alias);

                    // add all parent AND hasOne joins to the column list
                    $columns[] = "[$alias].*";
                    break;

                // stop processing these types of joins with the main query.  They might return "n" number of related records
//                case Relation::HAS_MANY_THROUGH:
//                    $alias2 = $alias . '_intermediate';
//                    $left1 = $modelNameSpace . $this->model->getModelName() . '.' . $relation->getFields();
//                    $right1 = "[$alias2]." . $relation->getIntermediateFields();
//                    $query->leftJoin($relation->getIntermediateModel(), "$left1 = $right1", $alias2);
//
//                    $left2 = "[$alias2]." . $relation->getIntermediateReferencedFields();
//                    $right2 = "[$alias]." . $relation->getReferencedFields();
//                    $query->leftJoin($referencedModel, "$left2 = $right2", $alias);
//                    break;

                default:
                    $this->di->get('logger')->warning("Relationship was ignored during join: {$this->model->getModelName()}.$alias, type #$type");
            }

            // attempt to join in side loaded belongsTo records
            // add all parent AND hasOne joins to the column list
            if ($type == Relation::BELONGS_TO) {
                $columns[] = "[$alias].*";
            }
        }
        $query->columns($columns);
        return $query;
    }
523167
public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*'])
	{
		if ($parent->getQuery()->from == $query->getQuery()->from) {
			return $this->getRelationQueryForSelfJoin($query, $parent, $columns);
		}

		$this->setJoin($query);

		return parent::getRelationQuery($query, $parent, $columns);
	}
866217
protected function setKeysForSaveQuery(Builder $query)
    {
        $query->where($this->foreignKey, $this->getAttribute($this->foreignKey));

        return $query->where($this->relatedKey, $this->getAttribute($this->relatedKey));
    }
417040
public static function getTreeByQuery(EloquentBuilder $query, array $columns = ['*'])
    {
        /**
         * @var Entity $instance
         */
        $instance = new static;
        $columns = $instance->prepareTreeQueryColumns($columns);
        return $query->get($columns)->toTree();
    }
46970
public function getRelationCountQueryForSelfRelation(Builder $query, Builder $parent)
	{
		$query->select(new Expression('count(*)'));

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

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

		return $query->where($hash.'.'.$query->getModel()->getKeyName(), '=', new Expression($key));
	}
875392
public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*'])
    {
        return $this->getRelationExistenceQuery($query, $parent, $columns);
    }
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));
	}