Resolve eager loaded relations from the model including any nested relations.

@param  \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model $data
@return array
196380
protected function resolveNestedRelations($data):array
    {
        if (is_null($data)) {
            return [];
        }

        $data = $data instanceof Model ? [$data] : $data;

        return collect($data)->flatMap(function ($model) {
            $relations = collect($model->getRelations());

            return $relations->keys()->merge($relations->flatMap(function ($relation, $key) {
                return collect($this->resolveNestedRelations($relation))->map(function ($nestedRelation) use ($key) {
                    return $key . '.' . $nestedRelation;
                });
            }));
        })->unique()->toArray();
    }
744865
public function find(array $data)
    {
        if (empty($data)) {
            return [];
        }
        
        $pks = [];
        foreach ($data as $item) {
            if (is_scalar($item)) {
                $pks[] = $item;
                continue;
            }
            
            if (is_object($item)) {
                $item = (array)$item;
            }
            if (is_array($item)) {
                foreach ($this->possiblePks as $possiblePk) {
                    if (array_key_exists($possiblePk, $item)) {
                        $pks[] = $item[$possiblePk];
                        break;
                    }
                }
            }
        }
        
        return $pks;
    }
1059585
public static function loadRelation(ActiveRecord $model, $relationName, $data, $requiredData = [])
    {
        $relationClass = $model->getRelation($relationName)->modelClass;
        return static::loadMultiple(
            @$data[(new $relationClass())->formName()],
            $relationClass,
            $model->$relationName,
            $requiredData
        );
    }
407223
protected function resolveModel($data):Model
    {
        if ($data instanceof Model) {
            return $data;
        }

        $model = array_values($data)[0];
        if (! $model instanceof Model) {
            throw new InvalidArgumentException('You can only transform data containing Eloquent models.');
        }

        return $model;
    }
853181
public function with($data)
	{
		if (get_parent_class($data) === \Illuminate\Database\Eloquent\Model::class) {
			$this->data = [
				'source_id' => $data->id,
				'status' => $data->status
			];
		}

		else {
			$this->data = $data;
		}

		return $this;
	}
263655
protected function resolveUseVariables($data)
    {
        foreach ($data as $key => $value) {
            $data[$key] = $this->getRestoredPropertyValue($value);
        }

        return $data;
    }
403749
protected function parseRelationships(array $data)
    {
        $result = [];
        foreach ($data as $name => $relationshipData) {
            $dataSegment  = $relationshipData !== null ?
                $this->getArrayValue($relationshipData, DocumentInterface::KEYWORD_DATA, null) : null;

            if ($dataSegment === null || (empty($dataSegment) === true && is_array($dataSegment) === true)) {
                $result[$name] = new RelationshipsObject($dataSegment);
                continue;
            }

            if (is_array($dataSegment) === false) {
                $this->errors->addRelationshipError($name, 'Invalid element.');
                continue;
            }

            if ($this->isProbablySingleIdentity($dataSegment) === true) {
                $parsed = $this->parseSingleIdentityInRelationship($name, $dataSegment);
            } else {
                $parsed = $this->parseArrayOfIdentitiesInRelationship($name, $dataSegment);
            }

            if ($parsed !== null) {
                $result[$name] = new RelationshipsObject($parsed);
            }
        }

        return $result;
    }
1307616
protected function getRelationshipDataRepresentation(RelationshipDataInterface $data): ?array
    {
        if ($data->isResource() === true) {
            return $this->getIdentifierRepresentationFromResource($data->getResource());
        } elseif ($data->isIdentifier() === true) {
            return $this->getIdentifierRepresentation($data->getIdentifier());
        } elseif ($data->isCollection() === true) {
            $representation = [];
            foreach ($data->getIdentifiers() as $identifier) {
                \assert($identifier instanceof IdentifierInterface);
                $representation[] = $this->getIdentifierRepresentation($identifier);
            }

            return $representation;
        }

        \assert($data->isNull() === true);

        return null;
    }
691576
protected function determineMorphModelsForRelationData(ModelRelationData $data)
    {
        // If models were set during analysis, trust them
        if ($data->morphModels && count($data->morphModels)) {
            return $data->morphModels;
        }

        // We do not have access to modelinformation here, so
        // if it cannot be derived from data, the enrichment step must handle this using context info.
        return [];
    }
382274
public static function findRelations($modelOne, $modelTwo, $keys): array
    {
        $keys = (array)$keys;
        if (!$relations = self::getRelations($modelOne, $modelTwo)) {
            throw new RelationNotFoundException("Relations between model `$modelOne` and `$modelTwo` is not defined");
        }

        /* @var Table $tableOneClass name */
        $tableOneClass = self::getModelClass($modelOne);

        /* @var string $tableOneName */
        $tableOneName = $tableOneClass::getInstance()->getName();

        /* @var Table $tableTwoClass name */
        $tableTwoClass = self::getModelClass($modelTwo);

        /* @var string $tableTwoName */
        $tableTwoName = $tableTwoClass::getInstance()->getName();

        /* @var Query\Select $tableTwoSelect */
        $tableTwoSelect = $tableTwoClass::getInstance()::select();

        // check many to many relation
        if (\is_int(\array_keys($relations)[0])) {
            // many to many relation over third table
            $modelThree = $relations[0];

            // relations between target table and third table
            $relations = self::getRelations($modelTwo, $modelThree);

            /* @var Table $tableThreeClass name */
            $tableThreeClass = self::getModelClass($modelThree);

            /* @var string $tableTwoName */
            $tableThreeName = $tableThreeClass::getInstance()->getName();

            // join it to query
            $tableTwoSelect->join(
                $tableTwoName,
                $tableThreeName,
                $tableThreeName,
                $tableTwoName . '.' . $relations[$modelTwo] . '=' . $tableThreeName . '.' . $relations[$modelThree]
            );

            // relations between source table and third table
            $relations = self::getRelations($modelOne, $modelThree);

            // join it to query
            $tableTwoSelect->join(
                $tableThreeName,
                $tableOneName,
                $tableOneName,
                $tableThreeName . '.' . $relations[$modelThree] . '=' . $tableOneName . '.' . $relations[$modelOne]
            );

            // set source keys
            $tableTwoSelect->where($tableOneName . '.' . $relations[$modelOne] . ' IN (?)', $keys);
        } else {
            // set source keys
            $tableTwoSelect->where($relations[$modelTwo] . ' IN (?)', $keys);
        }
        return $tableTwoSelect->execute();
    }
1161189
public function provide($data = null, array $options = [])
    {
        $relatedProperties = $this->_getRelatedProperties($options['fieldDefinitions']->getLimit(), (string)$data);

        $table = $this->config->getTable();
        $entity = $options['entity'];
        $association = $options['association'];
        $contactsData = TableRegistry::getTableLocator()->get($entity->getSource())->get($entity->get('id'), ['contain' => $association->getName()]);
        $associationData = $contactsData[$association->getProperty()];
        $associationList = [];
        if (!empty($associationData)) {
            foreach ($associationData as $associationRow) {
                $associationList[$associationRow->get('id')] = $associationRow->get($relatedProperties['displayField']);
            }
        }

        if (empty($associationList)) {
            return $data;
        }

        $elementName = 'CsvMigrations.FieldHandlers/BelongsToManyFieldHandler/view';
        $params = [
            'relatedProperties' => $relatedProperties,
            'options' => $options,
            'associationList' => $associationList
        ];

        return $this->renderElement($elementName, $params);
    }
390427
public static function findRelation($row, $relation): array
    {
        $model = $row->getTable()->getModel();

        /** @var \Bluz\Db\Table $relationTable */
        $relationTable = self::getModelClass($relation);
        $relationTable::getInstance();

        if (!$relations = self::getRelations($model, $relation)) {
            throw new RelationNotFoundException(
                "Relations between model `$model` and `$relation` is not defined"
            );
        }

        // check many-to-many relations
        if (\count($relations) === 1) {
            $relations = self::getRelations($model, current($relations));
        }

        $field = $relations[$model];
        $key = $row->{$field};

        return self::findRelations($model, $relation, [$key]);
    }
1040750
protected function parseScalarResult($data)
    {
        if (\is_array($data)) {
            $data = reset($data);
        }

        return [
            'id' => (string) $data,
            'text' => (string) $data,
        ];
    }
908250
public function createMany(array $data, $modelClass = null)
    {
        $models = [];
        foreach ($data as $item) {
            $models[] = $this->fromArray($item, $modelClass);
        }

        return $models;
    }
708053
private function getAssociationsBySearchData(array $data) : array
    {
        $fields = [];
        if (array_key_exists('criteria', $data)) {
            foreach (array_keys($data['criteria']) as $field) {
                $field = $this->table->aliasField((string)$field);
                if (! in_array($field, $fields)) {
                    $fields[] = $field;
                }
            }
        }

        if (array_key_exists('display_columns', $data)) {
            foreach ($data['display_columns'] as $field) {
                if (is_string($field) && ! in_array($field, $fields)) {
                    $fields[] = $field;
                }
            }
        }

        $result = [];
        foreach ($fields as $field) {
            list($name) = explode('.', $this->table->aliasField($field), 2);

            if ($name === $this->table->getAlias()) {
                continue;
            }

            if (! $this->table->hasAssociation($name)) {
                throw new \RuntimeException(sprintf('Table "%s" does not have association "%s"', $this->table->getAlias(), $name));
            }

            $association = $this->table->getAssociation($name);

            if (array_key_exists($association->getName(), $result)) {
                continue;
            }

            $result[$association->getName()] = $association;
        }

        return $result;
    }
293667
protected function relationsNestedUnder($relation)
    {
        $nested = [];

        // We are basically looking for any relationships that are nested deeper than
        // the given top-level relationship. We will just check for any relations
        // that start with the given top relations and adds them to our arrays.
        foreach ($this->eagerLoad as $name => $constraints) {
            if ($this->isNestedUnder($relation, $name)) {
                $nested[substr($name, strlen($relation.'.'))] = $constraints;
            }
        }

        return $nested;
    }
859386
public function determineFormStoreOptions(ModelRelationData $data)
    {
        $options = [];

        switch ($data->type) {

            case RelationType::MORPH_TO:
                // Set special morph options to mark the targetable model classes
                $models = $this->determineMorphModelsForRelationData($data);

                if (count($models)) {
                    $options['models'] = $models;
                }
                break;
        }

        return $options;
    }