Set the key names for the pivot model instance.

@param  string  $foreignKey
@param  string  $relatedKey
@return $this
18955
public function setPivotKeys($foreignKey, $relatedKey)
    {
        $this->foreignKey = $foreignKey;

        $this->relatedKey = $relatedKey;

        return $this;
    }
1342196
def set_pivot_keys(self, foreign_key, other_key):
        
        self.__foreign_key = foreign_key
        self.__other_key = other_key

        return self
931450
protected function relations($model, $foreignKeyIds)
    {
        $relations = $model->getSoftCascade();

        if (empty($relations)) {
            return;
        }

        foreach ($relations as $relation) {
            extract($this->relationResolver($relation));
            $this->validateRelation($model, $relation);

            $modelRelation = $model->$relation();

            /**
             * Maintains compatibility fot get foreign key name on laravel old and new methods.
             *
             * @link https://github.com/laravel/framework/issues/20869
             */
            $fnUseGetForeignKey = array_intersect($this->fnGetForeignKey, get_class_methods($modelRelation));
            $fnUseGetForeignKey = reset($fnUseGetForeignKey);

            //Get foreign key and foreign key ids
            $foreignKeyUse = $modelRelation->{$fnUseGetForeignKey}();
            $foreignKeyIdsUse = $foreignKeyIds;

            //Many to many relations need to get related ids and related local key
            if ($modelRelation instanceof BelongsToMany) {
                extract($this->getBelongsToManyData($modelRelation, $foreignKeyUse, $foreignKeyIds));
            } elseif ($modelRelation instanceof MorphOneOrMany) {
                extract($this->getMorphManyData($modelRelation, $foreignKeyIds));
            }

            $affectedRows = $this->affectedRows($modelRelation, $foreignKeyUse, $foreignKeyIdsUse);

            if ($action === 'restrict' && $affectedRows > 0) {
                DB::rollBack(); //Rollback the transaction before throw exception
                throw (new SoftCascadeRestrictedException())->setModel(get_class($modelRelation->getModel()), $foreignKeyUse, $foreignKeyIdsUse->toArray());
            }

            $this->execute($modelRelation, $foreignKeyUse, $foreignKeyIdsUse, $affectedRows);
        }
    }
663830
public function isForeignKeyIgnored($foreignKey)
    {
        // do not create entities for many2many tables
        if ($this->getConfig()->get(FormatterInterface::CFG_SKIP_M2M_TABLES) && $foreignKey->getReferencedTable()->isManyToMany()) {
            return true;
        }

        return false;
    }
618635
public function forgerForeignKey(AbstractForeignKey $foreignKey)
    {
        foreach ($this->foreignKeys as $name => $foreignSchema) {
            if ($foreignSchema == $foreignKey) {
                unset($this->foreignKeys[$name]);
                break;
            }
        }
    }
259098
public function hasManyThroughSeveral(array $relations, $foreignKey = null, $localKey = null)
    {
        $helpers = new Helpers($this);
        $currentRelation = current($relations);
        $related = $helpers->getClassNameFromCurrent($currentRelation, $relations);
        $firstRelation = new $related();

        $instance = $this->newRelatedInstance($related);
        $query = $instance
            ->newQuery()
            ->distinct()
            ->select($firstRelation->getTable().'.*');

        $joins = $helpers->getReverseJoinsRelations($relations, $localKey);
        foreach ($joins as $join) {
            $query->join($join['table'], $join['key'], '=', $join['foreign_key']);
        }

        $query->addSelect($join['select_key']);

        $key = $localKey ?? $this->getTable().'.'.$this->getKeyName();
        $value = $this->{$this->getKeyName()};

        $foreignKey = $foreignKey ?? $join['foreign_key'];

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

        $query->where($key, $value);

        return new HasMany($query, $this, $foreignKey, $localKey);
    }
1025763
protected function createBelongsToManyRelationship(
        $primaryKey,
        iterable $foreignKeys,
        string $intTableName,
        string $intPrimaryKeyName,
        string $intForeignKeyName
    ): void {
        assert(is_string($primaryKey) === true || is_int($primaryKey) === true);

        try {
            $this->inTransaction(function () use (
                $intTableName,
                $intPrimaryKeyName,
                $intForeignKeyName,
                $primaryKey,
                $foreignKeys
            ): void {
                $connection = $this->getConnection();
                $query      = $connection->createQueryBuilder();

                $query->insert($intTableName)->values([$intPrimaryKeyName => '?', $intForeignKeyName => '?']);
                $statement = $connection->prepare($query->getSQL());

                foreach ($foreignKeys as $value) {
                    assert(is_string($value) === true || is_int($value) === true);
                    $statement->bindValue(1, $primaryKey);
                    $statement->bindValue(2, $value);
                    $statement->execute();
                }
            });
        } /** @noinspection PhpRedundantCatchClauseInspection */ catch (DBALException $exception) {
            $message = 'Belongs-to-Many relationship creation failed.';
            throw new RepositoryException($message, 0, $exception);
        }
    }
102640
public function addBelongsToManyRelationsToRelatedModels()
    {
        if (count($this->relatedModels) < 2) {
            throw new \Exception(
                'Pivot table '.$this->table.' does not have enough foreign keys for determining n-n relations'
            );
        }

        $foreignKeys = array_keys($this->relatedModels);

        // Relation n-n du modèle A vers le modèle B
        $this->relatedModels[$foreignKeys[0]]->belongsToMany(
            $this->relatedModels[$foreignKeys[1]],
            $this->table,
            $foreignKeys[0],
            $foreignKeys[1]
        );

        // Relation n-n du modèle B vers le modèle A
        $this->relatedModels[$foreignKeys[1]]->belongsToMany(
            $this->relatedModels[$foreignKeys[0]],
            $this->table,
            $foreignKeys[1],
            $foreignKeys[0]
        );
    }
1224827
protected function hasOne(string $model, ?string $foreignKey = null): HasOne
	{
		$related = new $model;

		return new HasOne($related->getConnection(), $this, $related, $foreignKey);
	}
1207686
public function manyToOne ($relatedTable, $relatedKey = null, $key = null, $relationIdentifier = null)
		{
			if (is_null($relationIdentifier)) {
				$relationIdentifier = $relatedTable;
			}

			$this->relations[$relationIdentifier] = new ManyToOne(
				$this,
				$this->schema->getTable($relatedTable, true),
				$key,
				$relatedKey
			);

			return $this;
		}
321670
public function belongsToMany(
        $entity,
        string $related,
        string $table = null,
        string $foreignKey = null,
        string $otherKey = null
    ): BelongsToMany {
        // 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);
        $this->addPivotRelation($relation);

        // 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.
        $foreignKey = $foreignKey ?: $this->getForeignKey();

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

        $relatedMap = $relatedMapper->getEntityMap();

        $otherKey = $otherKey ?: $relatedMap->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($relatedMap);
        }

        return new BelongsToMany($relatedMapper, $entity, $table, $foreignKey, $otherKey, $relation);
    }
1134791
public function hasMany($related, $foreignKey = null, $localKey = null)
    {
        $this->htmlType('select');
        $args = func_get_args();
        $args['type'] = 'hasMany';
        return $this->setRelationship(json_encode($args));
    }
128935
public function setOriginal($data, $relatedKeyName)
    {
        if (empty($data)) {
            return $this;
        }

        foreach ($data as $value) {
            /*
             * like $this->original[30] = [ id = 30, .....]
             */
            $this->original[$value[$relatedKeyName]] = $value;
        }

        return $this;
    }
242103
function BelongsToMany(model, related, pivot, foreignPivotKey, relatedPivotKey, parentKey, relatedKey) {
        var _this = _super.call(this, model) /* istanbul ignore next */ || this;
        _this.related = _this.model.relation(related);
        _this.pivot = _this.model.relation(pivot);
        _this.foreignPivotKey = foreignPivotKey;
        _this.relatedPivotKey = relatedPivotKey;
        _this.parentKey = parentKey;
        _this.relatedKey = relatedKey;
        return _this;
    }
721707
public function setForeignKey($tableName, $columnName)
    {
        if ($tableName && $columnName) {
            $this->relatedTableName = $tableName;
            $this->relatedColumnName = $columnName;
        } else {
            $this->relatedTableName = "";
            $this->relatedColumnName = "";
        }
    }
1192757
public function addAsRelationSource(ForeignKey $foreignKey)
    {
        $foreignKey->isSource(true);
        $this->relationSources[] = $foreignKey;

        return $this;
    }
925555
protected function hasOneOrManyDeepLocalKeys(Model $related, array $throughParents, array $localKeys)
    {
        foreach (array_merge([$this], $throughParents) as $i => $instance) {
            /** @var \Illuminate\Database\Eloquent\Model $instance */
            if (! isset($localKeys[$i])) {
                if ($instance instanceof Pivot) {
                    $localKeys[$i] = ($throughParents[$i] ?? $related)->getForeignKey();
                } else {
                    $localKeys[$i] = $instance->getKeyName();
                }
            }
        }

        return $localKeys;
    }