Sets the foreign keys to null on a given model.

@param Model    $model
@param Relation $relation
@param string[] $keys
438852
protected function setForeignKeysToNull(Model $model, Relation $relation, array $keys)
    {
        if ( ! count($keys)) return;

        $key           = $this->normalizeForeignKey(head($keys));
        $model->{$key} = null;

        if (    count($keys) > 1
            &&  (   $relation instanceof MorphTo
                ||  $relation instanceof MorphOne
                ||  $relation instanceof MorphMany
            )
        ) {
            $key           = $this->normalizeForeignKey(array_values($keys)[1]);
            $model->{$key} = null;
        }

        $model->save();
    }
650952
protected function getRelationNameFromRelationInstance(Relation $relation)
    {
        // Get all relations, load instances and compare them loosely
        foreach ($this->info->relations as $relationData) {

            if ($relation == $this->model()->{$relationData->method}()) {
                return $relationData->method;
            }
        }

        // @codeCoverageIgnoreStart
        return null;
        // @codeCoverageIgnoreEnd
    }
924080
private function _checkRelationExists(Model $model, string $relationName)
    {
        if (!method_exists($model, $relationName) || !$model->{$relationName}() instanceof Relation) {
            if (Lang::has('resource-controller.data2relationinexistent')) {
                $message = trans('resource-controller.data2relationinexistent', ['relationName' => $relationName]);
            } else {
                $message = "Array type request data '{$relationName}' must be named after an existent relation.";
            }

            throw new MassAssignmentException($message);
        }
    }
412190
public function setListParent($relation, $recordKey = null)
    {
        if (false !== $relation && empty($relation)) {
            return $this->clearListParent();
        }

        if (false === $relation) {
            session()->put($this->getSessionKey(static::TYPE_PARENT), static::PARENT_DISABLE);
        } else {
            session()->put($this->getSessionKey(static::TYPE_PARENT), $relation . ':' . $recordKey);
        }

        return $this;
    }
1213718
public function matchOne(array $models, array $results, $relation)
    {
        $dictionary = $this->buildDictionary($results);

        foreach ($models as $model) {
            $key = $model->getAttribute($this->localKey);

            if (isset($dictionary[$key])) {
                $value = $this->getRelationValue($dictionary, $key, 'one');

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

        return $models;
    }
1038836
public function getEmptyValueForRelationship(string $relation)
    {
        if ($this->isSingleRelationship($relation)) {
            return;
        }

        if ($this->isManyRelationship($relation)) {
            return new Collection();
        }

        throw new MappingException("Cannot determine default value of $relation");
    }
1063101
protected function assignRelations($relations)
    {
        foreach ($relations as $relation) {
            if (!$relation instanceof RelationInterface) {
                throw new ModelException(sprintf('Relation must be an instance of RelationInterface, got "%s"', $this->getType($relation)));
            }

            foreach (array_keys($relation->keys()) as $field) {
                $this->assertField($field);
            }

            $this->relations[$relation->name()] = $relation;
        }
    }
876074
protected function addManyRelation(string $relation)
    {
        if (!in_array($relation, $this->manyRelations)) {
            $this->manyRelations[] = $relation;
        }
    }
261995
public static function validateToOneRelation($relation, $relationship)
    {
        if (
            !is_null($relation)
            && !$relation instanceof ResourceEntityInterface
        ) {
            $type = is_object($relation)
                ? get_class($relation) : gettype($relation);
            throw new \Exception(sprintf(
                self::ERROR_NOT_RESOURCE_ENTITY, $relationship, $type
            ));
        }
    }
998652
protected function cascadeRelation(Relation $relation, array $input, Model $parent = null)
	{
		// Make a child repository for containing the cascaded relationship through saves
		$target_model_class = get_class($relation->getQuery()->getModel());
		$relation_repository = (new self)->setModelClass($target_model_class);

		switch (get_class($relation)) {
			case BelongsTo::class:
				/**
				 * @var \Illuminate\Database\Eloquent\Relations\BelongsTo $relation
				 */
				// For BelongsTo, simply associate by foreign key.
				// (We don't have to assume the parent model exists to do this.)
				$related = $relation_repository->setInput($input)->save();
				$relation->associate($related);
				break;
			case HasMany::class:
				/**
				 * @var \Illuminate\Database\Eloquent\Relations\HasMany $relation
				 */
				// The parent model "owns" child models; any not specified here should be deleted.
				$current_ids = $relation->pluck($this->getKeyName())->toArray();
				$new_ids = array_filter(array_column($input, $this->getKeyName()));
				$removed_ids = array_diff($current_ids, $new_ids);
				if ( !empty($removed_ids)) {
					$relation->whereIn($this->getKeyName(), $removed_ids)->delete();
				}

				// Set foreign keys on the children from the parent, and save.
				foreach ($input as $sub_input) {
					$sub_input[$this->getRelationsForeignKeyName($relation)] = $parent->{$this->getKeyName()};
					$relation_repository->setInput($sub_input)->save();
				}
				break;
			case HasOne::class:
				/**
				 * @var \Illuminate\Database\Eloquent\Relations\HasOne $relation
				 */
				// The parent model "owns" the child model; if we have a new and/or different
				// existing child model, delete the old one.
				$current = $relation->getResults();
				if ( !is_null($current)
					&& ( !isset($input[$this->getKeyName()]) || $current->{$this->getKeyName()} !== intval($input[$this->getKeyName()]))
				) {
					$relation->delete();
				}

				// Set foreign key on the child from the parent, and save.
				$input[$this->getRelationsForeignKeyName($relation)] = $parent->{$this->getKeyName()};
				$relation_repository->setInput($input)->save();
				break;
			case BelongsToMany::class:
				/**
				 * @var \Illuminate\Database\Eloquent\Relations\BelongsToMany $relation
				 */
				// Find all the IDs to sync.
				$ids = [];

				foreach ($input as $sub_input) {
					$id = $relation_repository->setInput($sub_input)->save()->{$this->getKeyName()};

					// If we were passed pivot data, pass it through accordingly.
					if (isset($sub_input['pivot'])) {
						$ids[$id] = (array)$sub_input['pivot'];
					} else {
						$ids[] = $id;
					}
				}

				// Sync to save pivot table and optional extra data.
				$relation->sync($ids);
				break;
		}
	}
1080589
public function setRelation($relation, $relationColumn = null)
    {
        $this->relation = $relation;
        $this->relationColumn = $relationColumn;

        return $this;
    }
846681
public function setRelationScenario($relationName, $scenario)
    {
        /** @var BaseActiveRecord $owner */
        $owner = $this->owner;
        $relation = $owner->getRelation($relationName, false);
        if (in_array($relationName, $this->_relations) && !is_null($relation)) {
            $this->_relationsScenario[$relationName] = $scenario;
        } else {
            throw new InvalidArgumentException('Unknown ' . $relationName . ' relation');
        }

    }
178503
public function initRelation(array $models, $relation)
    {
        foreach ($models as $model) {
            $model->setRelation($relation, []);
        }
        return $models;
    }
719492
public function detach($model, string $relation, Model $value)
    {
        if (! $model instanceof Model) {
            $model = $this->find($model);
        }

        $model->$relation()->detach($value);
    }
1116260
public static function setRelations($modelOne, $modelTwo, $relations): void
    {
        $name = [$modelOne, $modelTwo];
        sort($name);
        $name = implode(':', $name);
        // create record in static variable
        self::$relations[$name] = $relations;
    }
653619
protected function hasOneOrManyDeepRelationMethod(Relation $relation)
    {
        $classes = [
            BelongsTo::class,
            HasManyDeep::class,
            HasManyThrough::class,
            MorphOneOrMany::class,
            HasOneOrMany::class,
            MorphToMany::class,
            BelongsToMany::class,
        ];

        foreach ($classes as $class) {
            if ($relation instanceof $class) {
                return 'hasOneOrManyDeepFrom'.class_basename($class);
            }
        }

        throw new RuntimeException('This relationship is not supported.'); // @codeCoverageIgnore
    }
681889
protected function loadRelatedModels(Model $model) {
        // get the relations to load
        $relations = $this->exposedRelationsFromRequest($model);

        foreach ($relations as $relation) {
            // if this relation is loaded via a method, then call said method
            if (in_array($relation, $model->relationsFromMethod())) {
                $model->$relation = $model->$relation();
                continue;
            }

            $model->load($relation);
        }
    }