@param array $data
@param array $relationships

@return array
334901
protected function fillRelationships($data, $relationships)
    {
        if ($this->isCollection($data)) {
            foreach ($relationships as $key => $relationship) {
                $data = $this->fillRelationshipAsCollection($data, $relationship, $key);
            }
        } else { // Single resource
            foreach ($relationships as $key => $relationship) {
                $data = $this->fillRelationshipAsSingleResource($data, $relationship, $key);
            }
        }

        return $data;
    }
926408
public function havingRelations(array $relations)
    {
        $instance = $this->havingStrategyInstance();

        $instance->havingStrategy()->havingRelations($relations);

        return $instance;
    }
407756
protected function buildEmbeddedRelationships(array $results) : array
    {
        $entityMap = $this->entityMap;
        $instance = $this->mapper->newInstance();
        $embeddeds = $entityMap->getEmbeddedRelationships();

        foreach ($embeddeds as $embedded) {
            $results = $entityMap->$embedded($instance)->match($results, $embedded);
        }

        return $results;
    }
463225
protected function hasParent($id, array $relationships)
    {
        foreach ($relationships as $parentId => $children) {
            // 0 is reserved, therefore must be ignored
            if ($parentId == 0) {
                continue;
            }

            if (in_array($id, $children)) {
                return true;
            }
        }

        // By default
        return false;
    }
1344269
protected function convertDotNotatedRelations($relations)
    {
        $dottedRelations = [];
        foreach ($relations as $name => $joryData) {
            $exploded = explode('.', $name);

            if (count($exploded) > 1) {
                // There was a dot, add it to the subRelations
                $firstRelation = $exploded[0];
                unset($exploded[0]);

                // implode all next layers, they will be handled in the next parser
                $dottedRelations[$firstRelation][implode('.', $exploded)] = $joryData;

                unset($relations[$name]);
            }
        }

        foreach ($dottedRelations as $name => $subRelations) {
            if (!array_key_exists($name, $relations)) {
                // This relation doesn't already exists, create it
                $relations[$name] = ['rlt' => []];
            }

            foreach ($subRelations as $subName => $joryData) {
                if (array_key_exists('rlt', $relations[$name])) {
                    $relations[$name]['rlt'][$subName] = $joryData;
                } else {
                    $relations[$name]['relations'][$subName] = $joryData;
                }
            }
        }

        return $relations;
    }
305022
protected function parseRelationships($includedData)
    {
        $relationships = [];

        foreach ($includedData as $key => $inclusion) {
            foreach ($inclusion as $includeKey => $includeObject) {
                $relationships = $this->buildRelationships($includeKey, $relationships, $includeObject, $key);
                if (isset($includedData[0][$includeKey]['meta'])) {
                    $relationships[$includeKey][0]['meta'] = $includedData[0][$includeKey]['meta'];
                }
            }
        }

        return $relationships;
    }
84206
public function with($relationships)
    {
        foreach ($this->resources as $resource) {
            $resource->with($relationships);
        }

        return $this;
    }
7156
protected function updateRelation($relationsData)
    {
        foreach ($relationsData as $name => $values) {
            if (!method_exists($this->model, $name)) {
                continue;
            }

            $relation = $this->model->$name();

            $oneToOneRelation = $relation instanceof Relations\HasOne
                || $relation instanceof Relations\MorphOne
                || $relation instanceof Relations\BelongsTo;

            $prepared = $this->prepareUpdate([$name => $values], $oneToOneRelation);

            if (empty($prepared)) {
                continue;
            }

            switch (true) {
                case $relation instanceof Relations\BelongsToMany:
                case $relation instanceof Relations\MorphToMany:
                    if (isset($prepared[$name])) {
                        $relation->sync($prepared[$name]);
                    }
                    break;
                case $relation instanceof Relations\HasOne:

                    $related = $this->model->$name;

                    // if related is empty
                    if (is_null($related)) {
                        $related = $relation->getRelated();
                        $qualifiedParentKeyName = $relation->getQualifiedParentKeyName();
                        $localKey = Arr::last(explode('.', $qualifiedParentKeyName));
                        $related->{$relation->getForeignKeyName()} = $this->model->{$localKey};
                    }

                    foreach ($prepared[$name] as $column => $value) {
                        $related->setAttribute($column, $value);
                    }

                    $related->save();
                    break;
                case $relation instanceof Relations\BelongsTo:
                case $relation instanceof Relations\MorphTo:

                    $parent = $this->model->$name;

                    // if related is empty
                    if (is_null($parent)) {
                        $parent = $relation->getRelated();
                    }

                    foreach ($prepared[$name] as $column => $value) {
                        $parent->setAttribute($column, $value);
                    }

                    $parent->save();

                    // When in creating, associate two models
                    $foreignKeyMethod = (app()->version() < '5.8.0') ? 'getForeignKey' : 'getForeignKeyName';
                    if (!$this->model->{$relation->{$foreignKeyMethod}()}) {
                        $this->model->{$relation->{$foreignKeyMethod}()} = $parent->getKey();

                        $this->model->save();
                    }

                    break;
                case $relation instanceof Relations\MorphOne:
                    $related = $this->model->$name;
                    if (is_null($related)) {
                        $related = $relation->make();
                    }
                    foreach ($prepared[$name] as $column => $value) {
                        $related->setAttribute($column, $value);
                    }
                    $related->save();
                    break;
                case $relation instanceof Relations\HasMany:
                case $relation instanceof Relations\MorphMany:

                    foreach ($prepared[$name] as $related) {
                        /** @var Relations\Relation $relation */
                        $relation = $this->model()->$name();

                        $keyName = $relation->getRelated()->getKeyName();

                        $instance = $relation->findOrNew(Arr::get($related, $keyName));

                        if ($related[static::REMOVE_FLAG_NAME] == 1) {
                            $instance->delete();

                            continue;
                        }

                        Arr::forget($related, static::REMOVE_FLAG_NAME);

                        $instance->fill($related);

                        $instance->save();
                    }

                    break;
            }
        }
    }
394056
public static function unset2D(array $data, $keys) {
	    foreach ((array)$keys as $key => $item) {
	        if (!is_array($item)) {
	            unset($data[$item]);
	            continue;
            }
            if (!isset($data[$key]) && !array_key_exists($key, $data)) {
	            continue;
            }
            if (!is_array($data[$key])) {
                unset($data[$key]);
                continue;
            }
            foreach ($item as $value) {
	            unset($data[$key][$value]);
            }
        }
        return $data;
    }
388367
protected function byAssociations(array $data): array
    {
        $result = [];
        foreach ($this->getAssociationsBySearchData($data) as $association) {
            // skip non-supported associations
            if (!in_array($association->type(), Options::getSearchableAssociations())) {
                continue;
            }

            $targetTable = $association->getTarget();

            // skip associations with itself
            if ($targetTable->getTable() === $this->table->getTable()) {
                continue;
            }

            $primaryKeys = [];
            foreach ((array)$targetTable->getPrimaryKey() as $primaryKey) {
                $primaryKeys[] = $targetTable->aliasField($primaryKey);
            }

            // instantiate Search on related table
            $search = new Search($targetTable, $this->user);

            $select = array_diff($search->getSelectClause($data), $primaryKeys);
            if (!empty($select)) {
                $result[$association->getName()]['select'] = $select;
            }

            $where = $search->getWhereClause($data);
            if (!empty($where)) {
                $result[$association->getName()]['where'] = $where;
            }
        }

        return $result;
    }
686597
public function initRelation(array $models, $relation)
    {
        foreach ($models as $model)
        {
            // In the case of fetching nested relations, we will get an array
            // with the first key being the model we need, and the other being
            // the related model so we'll just take the first model out of the array.
            if (is_array($model)) $model = reset($model);

            $model->setRelation($relation, null);
        }

        return $models;
    }
754161
protected function addMissingParentRelations(array $relations)
    {

        $filled = $relations;

        foreach ($relations as $relation) {

            $segments = explode('.', $relation);
            $path = [];
            foreach ($segments as $segment) {
                $path[] = $segment;
                $pathString = implode('.', $path);
                if (!in_array($pathString, $relations)) {
                    $filled[] = $pathString;
                }
            }
        }

        return $filled;

    }
126953
public function addRelations($relations)
	{
		if(is_numeric($relations))
		{
			
			$relations = intval($relations);
			$this->relations = $relations;
		}
		else
		{
			$relations = ( is_array($relations) ) ? $relations : explode(',', strval($relations));
			foreach ($relations as $prop)
			{
				if( ! in_array($prop, $this->relations) )
				{
					$this->relations[] = trim($prop);
				}
			}
		}

		if($this instanceof Collection)
		{
			foreach ($this->data as $model)
			{
				$model->addRelations($this->relations);
			}
		}
	}
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();
    }
239707
public function getNonExistingRelated(array $relationships): array
    {
        $nonExisting = [];

        foreach ($relationships as $relation) {
            if ($this->hasAttribute($relation) && array_key_exists($relation, $this->relationships)) {
                $nonExisting = array_merge($nonExisting, $this->getNonExistingFromRelation($relation));
            }
        }

        return $nonExisting;
    }
315274
protected function relationshipsToArray(): array
    {
        $relationships = [];

        foreach ($this->relationships as $name => $relationship)
        {
            $relationships[$name] = $relationship->toArray();
        }

        return $relationships;
    }
1236587
public function loadRelations($data)
    {
        /** @var BaseActiveRecord $owner */
        $owner = $this->owner;
        foreach ($this->_relations as $relationName) {
            $keyName = $this->_getRelationKeyName($relationName);
            if (array_key_exists($keyName, $data)) {
                $owner->{$relationName} = $data[$keyName];
            }
        }
    }