Resolves a relation based on the key provided,
either on the current model or a provided model instance.

@param string $key
@param mixed  $model

@return mixed
963074
public function relatedKey($key, $model = false)
    {
        if (!$model) {
            $model = $this->model;
        }

        if (($methodBreaker = strpos($key, '.')) !== false) {
            $method = substr($key, 0, $methodBreaker);
            if (method_exists($model, $method)) {
                if (method_exists($model->$method, $submethod = str_replace($method.'.', '', $key))) {
                    return $model->$method->$submethod();
                }

                if (isset($model->$method->$submethod)) {
                    return $model->$method->$submethod;
                }

                return $model->getRelationValue($method);
            }
        }

        return false;
    }
445416
protected function getValueFromModel($model)
    {
        if ( ! ($model instanceof Model)) {
            return null;
        }

        return get_class($model)
             . static::CLASS_AND_KEY_SEPARATOR
             . $model->getKey();
    }
702775
protected function resolveQueryBuilderFromModel($model)
    {
        if ($this->isEloquentModel($model)) {
            return $model->newQuery();
        } elseif ($this->isEloquentRelationModel($model)) {
            return $model->getQuery();
        } elseif (! $this->isQueryBuilder($model)) {
            throw new InvalidArgumentException('Unable to load Query Builder from $model');
        }

        return $model;
    }
417785
public function modelHasLanguage($model, $key)
    {
        if ($this->isLanguage($key)) {
            return true;
        }

        if ($model->translations->get($key)) {
            return true;
        }

        return $this->model->language === $key;
    }
429912
protected function handleKey($key, Model $model)
    {
        if (is_string($key) === true) {
            $model->setName($this->pool->encodingService->encodeString($key))
                ->setConnectorType(Connectors::ASSOCIATIVE_ARRAY);

            return;
        }

        $model->setName($key)->setConnectorType(Connectors::NORMAL_ARRAY);
    }
467465
public function get($key) {
        $value = null;

        // If the key already exists in the attribute list, return it immediately
        if ($this->has($key)) {
            $value = $this->_attributes[$key];
        }

        // If the key being accessed points to a relation, either lazy load the data or return the cached data
        if (!$value && $this->hasRelation($key)) {
            $value = $this->getRelation($key)->fetchRelation();

            $this->set($key, $value);

        // If an accessor has been defined, run the value through the accessor before returning
        } else if ($method = $this->hasAccessor($key)) {
            $value = $this->{$method}($value);
        }

        return $value;
    }
730676
public function getAttribute($key) {
        $attr = parent::getAttribute($key);

        if ($attr === null) {
            $camelKey = camel_case($key);
            if (array_key_exists($camelKey, static::$relationsData)) {
                $this->relations[$key] = $this->$camelKey()->getResults();
                return $this->relations[$key];
            }
        }

        return $attr;
    }
1149844
protected function resolveAttribute($model, $attribute)
    {
        $result = $this->mapToClass($model->{$this->attribute . '_type'});

        foreach ($this->meta['types'] as $type) {

            $relatedModel = new $type['value'];

            if($this->mapToKey($type['value']) == $model->{$this->attribute . '_type'}) {
                $relatedModel = ($model->{$this->attribute}) ? $model->{$this->attribute}
                     : $relatedModel->newQuery()->findOrFail($model->{$this->attribute . '_id'});
            }

            foreach ($type['fields'] as $field) {
                $field->resolve($relatedModel);
            }

        }

        return $result;
    }
238852
public function fromModel(string $model, string $name, string $key = null): self
    {
        $key = $key ?? (new $model())->getModel()->getKeyName();

        $this->set('relationModel', Crypt::encryptString($model));
        $this->set('relationName', Crypt::encryptString($name));
        $this->set('relationKey', Crypt::encryptString($key));

        $this->addBeforeRender(function () use ($name, $key) {
            $value = $this->get('value');

            if (! is_countable($value)) {
                $value = Arr::wrap($value);
            }

            $value = collect($value)
                ->map(function ($item) use ($name, $key) {
                    return [
                        'id'   => $item->$key,
                        'text' => $item->$name,
                    ];
                })->toJson();

            $this->set('value', $value);
        });

        return $this;
    }
1175365
public function resolveModelClassName($model)
    {
        foreach ($this->metadataFactory->getAllMetadata() as $metadata) {
            /** @var Mapping\ClassMetadata $metadata */
            foreach ($metadata->getFieldMappings() as $mapping) {
                if (is_a($model, $mapping->getName())) {
                    return $metadata->getName();
                }
            }
        }

        return null;
    }
365983
protected function fetchDefinition(ModelInterface $model, $relation)
    {
        if ($model->hasRelation($relation)) {
            return $model->relation($relation);
        }

        throw new RelationException(sprintf('Unable to resolve relation "%s" not found in model "%s"', $relation, $model->entity()));
    }
770621
protected function addOrGetRelated($key)
    {
        $related = $this->relatedValues->get($key) ?? $this->getFreshModelForElements();

        if (! $related->exists && ! $this->exceedsLimit()) {
            $this->relatedValues->put($key, $related);
        }

        return $related;
    }
528823
public function retrieve(Model $model, $source)
    {
        $this->model = $model;

        if ($this->isTranslated()) {

            $keys      = [];
            $localeKey = config('translatable.locale_key', 'locale');

            foreach ($model->translations as $translation) {
                /** @var Relation $relation */
                $relation = $translation->{$source}();

                $keys[ $translation->{$localeKey} ] = $this->getValueFromRelationQuery($relation);
            }

            return $keys;
        }

        $relation = $this->resolveModelSource($model, $source);

        if ( ! ($relation instanceof Relation)) {
            throw new UnexpectedValueException(
                "{$source} did not resolve to a relation for " . get_class($this) . " on " . get_class($model)
            );
        }

        try {
            return $this->getValueFromRelationQuery($relation);

        } catch (\Exception $e) {

            throw new RuntimeException(
                "Exception retrieving relation values for source '{$source}' "
                . "for form field key '{$this->formFieldData->key}'",
                0,
                $e
            );
        }
    }
1344561
public function getPrimaryKey($model)
    {
        // describe the model
        $desc = $this->desc($model);

        // search by fields for primary key
        foreach ($desc[$model] as $field => $aspects) {
            if ($aspects['Key'] == 'PRI') {
                return $field;
            }
        }
    }
953538
public function getRelationValue( $key )
    {
        if( $response = parent::getRelationValue( $key ) ) {
            return $response;
        }

        if( isset( static::$relationshipMacros[ $key ] ) ) {
            return $this->loadRelationshipMacro( $key );
        }
    }
1082282
public function routeToViewModelTranslation($key, $model)
    {
        if ($translation = $model->translations->get($key)) {
            return $this->currentUrl('view/'.$translation->id);
        }
    }
442732
public function getRelationship($key)
    {
        if (isset($this->relationships[$key])) {
            return $this->relationships[$key];
        }
        return null;
    }