Get either saving post data for related models or models themself
@param $relation
@return array
960573
public function getRelationSavingModels($relation)
    {
        $result = isset($this->savingModels[$relation])
            ? $this->savingModels[$relation]
            : $this->owner->$relation;
        return is_array($result) && (!$this->owner->getRelation($relation)->multiple)
            ? reset($result) : $result;
    }
504859
protected function joinEagerLoadedColumn($relation, $relationColumn)
    {
        $joins = [];
        foreach ((array) $this->getQueryBuilder()->joins as $key => $join) {
            $joins[] = $join->table;
        }

        $model = $this->query->getRelation($relation);
        if ($model instanceof BelongsToMany) {
            $pivot   = $model->getTable();
            $pivotPK = $model->getForeignKey();
            $pivotFK = $model->getQualifiedParentKeyName();

            if (!in_array($pivot, $joins)) {
                $this->getQueryBuilder()->leftJoin($pivot, $pivotPK, '=', $pivotFK);
            }

            $related = $model->getRelated();
            $table   = $related->getTable();
            $tablePK = $related->getForeignKey();
            $tableFK = $related->getQualifiedKeyName();

            if (!in_array($table, $joins)) {
                $this->getQueryBuilder()->leftJoin($table, $pivot . '.' . $tablePK, '=', $tableFK);
            }
        } else {
            $table   = $model->getRelated()->getTable();
            $foreign = $model->getQualifiedForeignKey();
            $other   = $model->getQualifiedOtherKeyName();
            if (!in_array($table, $joins)) {
                $this->getQueryBuilder()->leftJoin($table, $foreign, '=', $other);
            }
        }

        $column = $table . '.' . $relationColumn;

        return $column;
    }
484716
protected function parseSingleRelationship(string $relation): bool
    {
        if (!$value = $this->parseForCommonValues($relation)) {
            return true;
        }

        if ($value instanceof Collection || is_array($value) || $value instanceof CollectionProxy) {
            throw new MappingException("Entity's attribute $relation should not be array, or collection");
        }

        if ($value instanceof LazyLoadingInterface && !$value->isProxyInitialized()) {
            $this->relationships[$relation] = [];

            return true;
        }

        // If the attribute is a loaded proxy, swap it for its
        // loaded entity.
        if ($value instanceof LazyLoadingInterface && $value->isProxyInitialized()) {
            $value = $value->getWrappedValueHolderValue();
        }

        if ($this->isParentOrRoot($value)) {
            $this->relationships[$relation] = [];

            return true;
        }

        // At this point, we can assume the attribute is an Entity instance
        // so we'll treat it as such.
        $subAggregate = $this->createSubAggregate($value, $relation);

        // Even if it's a single entity, we'll store it as an array
        // just for consistency with other relationships
        $this->relationships[$relation] = [$subAggregate];

        // We always need to check a loaded relation is in sync
        // with its local key
        $this->needSync[] = $relation;

        return true;
    }
588390
protected function getRelationshipRepresentation(RelationshipInterface $relationship): array
    {
        $representation = [];

        if ($relationship->hasLinks() === true) {
            $representation[DocumentInterface::KEYWORD_LINKS] =
                $this->getLinksRepresentation($this->getUrlPrefix(), $relationship->getLinks());
        }

        if ($relationship->hasData() === true) {
            $representation[DocumentInterface::KEYWORD_DATA] = $this->getRelationshipDataRepresentation(
                $relationship->getData()
            );
        }

        if ($relationship->hasMeta() === true) {
            $representation[DocumentInterface::KEYWORD_META] = $relationship->getMeta();
        }

        return $representation;
    }
74181
public function getRelation(string $relation) : Relationship
    {
        // We want to run a relationship query without any constrains so that we will
        // not have to remove these where clauses manually which gets really hacky
        // and is error prone while we remove the developer's own where clauses.
        $query = Relationship::noConstraints(function () use ($relation) {
            return $this->entityMap->$relation($this->mapper->newInstance());
        });

        $nested = $this->nestedRelations($relation);

        // If there are nested relationships set on the query, we will put those onto
        // the query instances so that they can be handled after this relationship
        // is loaded. In this way they will all trickle down as they are loaded.
        if (count($nested) > 0) {
            $query->getQuery()->with($nested);
        }

        return $query;
    }
1042681
public function relation($relation)
    {
        list($name, $furtherRelations) = $this->factory->splitRelationName($relation);

        if (!isset($this->relations[$name])) {
            throw new QueryException(sprintf('Unable to retrieve relation "%s" query, relation does not exists in query "%s"', $name, $this->model()->entity()));
        }

        if ($furtherRelations) {
            return $this->relations[$name]->relation($furtherRelations);
        }

        return $this->relations[$name];
    }
469232
protected function makeModelFormFieldDataForRelationData(ModelRelationData $relation, $key = null)
    {
        $required = (   in_array($relation->type, [
                            RelationType::BELONGS_TO,
                            RelationType::BELONGS_TO_THROUGH,
                            RelationType::MORPH_TO,
                        ])
                    &&  ! $relation->nullable_key);

        // Shows any relation, regardless of type. Note that this is not optimal for
        // records with a large number of to-many related items, for instance.

        return new ModelFormFieldData([
            'key'              => $key ?: $relation->name,
            'source'           => $relation->method,
            'required'         => $required,
            'translated'       => $relation->translated,
            'display_strategy' => $this->determineFormDisplayStrategyForRelation($relation),
            'store_strategy'   => $this->determineFormStoreStrategyForRelation($relation),
            'options'          => $this->determineFormStoreOptionsForRelation($relation),
        ]);
    }
160794
public function initRelation(array $models, $relation)
    {
        foreach ($models as $model) {
            $model->setRelation($relation, $this->getDefaultFor($model));
        }

        return $models;
    }
396422
protected function getRelatedAggregatesFromHashes(array $hashes, string $relation): array
    {
        $related = [];

        foreach ($hashes as $hash) {
            $aggregate = $this->getRelatedAggregateFromHash($hash, $relation);

            if ($aggregate) {
                $related[] = $aggregate;
            }
        }

        return $related;
    }
1190334
public function getRelationalDataAndModels($model, $desired_relation)
    {
        // $relations = $model->getRelations();
        $relation = $this->trimCommonRelationEndings($desired_relation);
        // Grab all the model relationships that don't return as a collection
        if (method_exists($model, $relation)) {
            // We have our relation, return it
            return $this->getResolvedRelationship($model, $relation);
        } elseif (method_exists($model, $relation.'s')) {
            // We have our relations, return it
            return $this->getResolvedRelationship($model, $relation.'s');
        } elseif (method_exists($model, $relation.'s')) {
            return $this->getResolvedRelationship($model, $relation.'s');
        }
        // don't return because clearly, nothing matches what we need.
    }
438694
protected function getRelationKeyNames(MorphToMany $relation)
    {
        // We need to get the keys of both tables from the relation class.
        // The method names have changed in Laravel 5.4 & again in 5.5,
        // so we will first check which methods are available to us.
        if (method_exists($relation, 'getForeignKey')) {
            return [
                $relation->getForeignKey(),
                $relation->getOtherKey(),
            ];
        }

        if (method_exists($relation, 'getQualifiedForeignKeyName')) {
            return [
                $relation->getQualifiedForeignKeyName(),
                $relation->getQualifiedRelatedKeyName(),
            ];
        }

        return [
            $relation->getQualifiedForeignPivotKeyName(),
            $relation->getQualifiedRelatedPivotKeyName(),
        ];
    }
1347427
public function has($relation)
    {
        $query = $this->make();
        return $query->has($relation)->get( $this->getParameters );
    }
756219
public function getResults($relation)
    {
        $result = $this->query->first();

        $this->cacheRelation($result, $relation);

        return $result;
    }
1350877
protected function getForeignKeyAttributesFromNonLoadedRelation(string $relation): array
    {
        $keys = $this->entityMap->getLocalKeys($relation);

        // We'll treat single and composite keys (polymorphic) the same way.
        if (!is_array($keys)) {
            $keys = [$keys];
        }

        $foreignKey = [];

        foreach ($keys as $key) {
            $foreignKey[$key] = $this->getEntityAttribute($key);
        }

        return $foreignKey;
    }
789311
public function getRelation($relation)
    {
        $query = Relation::noConstraints(function() use ($relation)
        {
            return $this->getModel()->$relation();
        });

        $nested = $this->nestedRelations($relation);

        if (count($nested) > 0) {
            $query->getQuery()->with($nested);
        }

        return $query;
    }
861097
protected function makeModelListColumnDataForRelationData(ModelRelationData $relation)
    {
        return new ModelListColumnData([
            'source'         => $relation->method,
            'strategy'       => $this->determineListDisplayStrategyForRelation($relation),
            'label'          => ucfirst(str_replace('_', ' ', snake_case($relation->method))),
            'sortable'       => false,
        ]);
    }
142525
public function loadRelation($relation)
    {
        //If the node isn't already loaded
        if(!isset($this->relationProxyCache[$relation->getId()]))
        {
            //Create a proxy entity
            $em = $this;
            $entity = $this->proxyFactory->fromRelation($relation, $this->metaRepository, function ($node) use ($em) {
                return $em->loadNode($node);
            });

            $this->relationProxyCache[$relation->getId()] = $entity;
            $this->everymanRelationCache[$this->getHash($entity)] = $relation;
        }

        return $this->relationProxyCache[$relation->getId()];
    }