Ajoute une relation HasMany vers un autre modèle.

@param  Model  $relatedModel
@param  string $foreignKey
@return void
5380
public function hasMany(Model $relatedModel, $foreignKey)
    {
        if ($relatedModel->isIgnored()) {
            return;
        }

        $this->relations->add(
            new Relations\HasMany($this, $relatedModel, $foreignKey)
        );
    }
1143708
public function hasMany($entity, string $related, string $foreignKey = null, string $localKey = null): HasMany
    {
        $foreignKey = $foreignKey ?: $this->getForeignKey();

        $relatedMapper = Manager::getInstance()->mapper($related);

        $localKey = $localKey ?: $this->getKeyName();

        // Add the relation to the definition in map
        list(, $caller) = debug_backtrace(false);
        $relation = $caller['function'];
        $this->relatedClasses[$relation] = $related;

        $this->addManyRelation($relation);
        $this->addForeignRelation($relation);

        return new HasMany($relatedMapper, $entity, $foreignKey, $localKey);
    }
247441
public function belongsToMany($alias, $class, $junction, $foreignKey = null, $relatedKey = null, Closure $conditions = null) {
        $relation = (new ManyToMany($alias, $class))
            ->setJunction($junction)
            ->setPrimaryForeignKey($foreignKey)
            ->setRelatedForeignKey($relatedKey);

        if ($conditions) {
            $relation->setConditions($conditions);
        }

        return $this->addRelation($relation);
    }
743181
public function belongsTo($related, $foreignKey = '')
    {
        if (!$foreignKey && $related instanceof Model) {
            $column = $related->getTable();
        } else {
            $column = $foreignKey;
        }

        if (empty($related)) {
            unset($this->bean->$column);
        } elseif ($related instanceof Model) {
            $this->bean->$column = $related->getBean();
        } else {
            $this->bean->$column = $related;
        }
    }
167001
public function belongsToMany($related, $table = null, $foreignKey = null, $relatedKey = null, $relation = null)
    {
        // If no relationship name was passed, we will pull backtraces to get the
        // name of the calling function. We will use that function name as the
        // title of this relation since that is a great convention to apply.
        if (is_null($relation)) {
            $relation = $this->guessBelongsToManyRelation();
        }

        // First, we'll need to determine the foreign key and "other key" for the
        // relationship. Once we have determined the keys we'll make the query
        // instances as well as the relationship instances we need for this.
        $instance = $this->newRelatedInstance($related);

        $foreignKey = $foreignKey ?: $this->getForeignKey();

        $relatedKey = $relatedKey ?: $instance->getForeignKey();

        // If no table name was provided, we can guess it by concatenating the two
        // models using underscores in alphabetical order. The two model names
        // are transformed to snake case from their default CamelCase also.
        if (is_null($table)) {
            $table = $this->joiningTable($related);
        }

        return new BelongsToMany(
            $instance->newQuery(), $this, $table, $foreignKey, $relatedKey, $relation
        );
    }
4860
public function setRelatedModel(Model $model) {
        $this->_relatedModel = $model;
        $this->setRelatedClass(get_class($model));

        return $this;
    }
298424
public function belongsTo(string $related, ?string $foreign_key = null, ?string $local_key = null)
    {
        $relatedModel = app()->make($related);
        $local_key = $local_key ?? $this->getKey();
        // Make the table name singular and append ID to it
        // FIXME: Use a more reliable approach
        $foreign_key = $foreign_key ?? rtrim($relatedModel->getTable(), 's').'_id';
        return new BelongsTo($relatedModel, $this, $foreign_key, $local_key);
    }
1224328
protected function doSetSkillId(Kstruktur $model, $relatedId) {
		if ($model->getSkillId() !== $relatedId) {
			$model->setSkillId($relatedId);

			return true;
		}

		return false;
	}
182979
protected function belongsTo(string $model, ?string $foreignKey = null): BelongsTo
	{
		$related = new $model;

		return new BelongsTo($related->getConnection(), $this, $related, $foreignKey);
	}
294898
public function hasOne($name, $foreignKey, $modelName = null)
    {
        $this->addAssociation('hasOne', $name, $foreignKey, $modelName);
        return $this;
    }
292046
public function constrainQuery(EloquentBuilder &$query, $relatedModel, $constraint)
	{
		//if the column hasn't been joined yet, join it
		if (!$this->validator->isJoined($query, $this->getOption('table')))
		{
			$query->join($this->getOption('table'), $relatedModel->getTable().'.'.$relatedModel->getKeyName(), '=', $this->getOption('column2'));
		}

		$query->where($this->getOption('table').'.'.$this->getOption('column'), '=', $constraint);
	}
1192757
public function addAsRelationSource(ForeignKey $foreignKey)
    {
        $foreignKey->isSource(true);
        $this->relationSources[] = $foreignKey;

        return $this;
    }
1280254
public function hasOneDeep($related, array $through, array $foreignKeys = [], array $localKeys = [])
    {
        return $this->newHasOneDeep(...$this->hasOneOrManyDeep($related, $through, $foreignKeys, $localKeys));
    }
40353
public function belongsTo( $related, $foreignKey = null, $otherKey = null, $relation = null )
	{
		// If no relation name was given, we will use this debug backtrace to extract
		// the calling method's name and use that as the relationship name as most
		// of the time this will be what we desire to use for the relationships.
		if ( is_null( $relation ) )
		{
			list( $current, $caller ) = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 );

			$relation = $caller['function'];
		}

		// If no foreign key was supplied, we can use a backtrace to guess the proper
		// foreign key name by using the name of the relationship function, which
		// when combined with an "_id" should conventionally match the columns.
		if ( is_null( $foreignKey ) )
		{
			$foreignKey = Str::snake( $relation ) . '_id';
		}

		$instance = new $related;

		// Once we have the foreign key names, we'll just create a new Eloquent query
		// for the related models and returns the relationship instance which will
		// actually be responsible for retrieving and hydrating every relations.
		$query = $instance->newQuery();

		$otherKey = $otherKey ?: $instance->getKeyName();

		return new BelongsTo( $query, $this, $foreignKey, $otherKey, $relation );
	}
1128457
protected function hasOne($associatedClass, $foreignKey = null, $mappingKeyInBaseTable = null)
    {
        return $this->findHasOneOrMany($associatedClass, $foreignKey, $mappingKeyInBaseTable);
    }
567627
public function setRelatedModel($foreignKey, Model $relatedModel)
    {
        if (count($this->relatedModels) >= 2
            && !array_key_exists($foreignKey, $this->relatedModels)) {

            return;
        }

        $this->relatedModels[$foreignKey] = $relatedModel;
    }
1169144
public function relatedItemDelete($resourceModel, $relatedId, RelatedRepository $repository)
    {
        //  perform find to invoke ModelNotFound exception
        $relationshipModel = $repository->findRelated($resourceModel, $relatedId);
        if ($relationshipModel === null) {
            throw (new ModelNotFoundException())->setModel(get_class($resourceModel));
        }

        $relation = $this->getRelation($resourceModel);

        $relation->detach($relatedId);
    }