@param $foreignKey

@return Relation
519103
public function getRelationByForeignKey($foreignKey)
    {
        if (!isset($this->relationsByForeignKey[ $foreignKey ])) {
            throw new \OutOfBoundsException("Undefined foreign key: {$foreignKey}");
        }

        return $this->relationsByForeignKey[ $foreignKey ];
    }
564048
public function hasOne(Model $relatedModel, $foreignKey)
    {
        if ($relatedModel->isIgnored()) {
            return;
        }

        $this->relations->add(
            new Relations\HasOne($this, $relatedModel, $foreignKey)
        );
    }
901754
public function hasOne($modelName, $foreignKey=null, $localKey=null)
    {
        return $this->_relationship($modelName, __FUNCTION__, $foreignKey, $localKey);
    }
989961
public function addReferrer(ForeignKey $fk)
    {
        if ($this->referrers === null) {
            $this->referrers = array();
        }
        $this->referrers[] = $fk;
    }
359895
public function getAddForeignKeyDDL(ForeignKey $fk)
    {
        if ($fk->isSkipSql() || $fk->isPolymorphic()) {
            return;
        }
        $pattern = "
ALTER TABLE %s ADD %s;
";

        return sprintf($pattern,
            $this->quoteIdentifier($fk->getTable()->getName()),
            $this->getForeignKeyDDL($fk)
        );
    }
82910
public function hasMany($modelName, $foreignKey=null, $localKey=null)
    {
        return $this->_relationship($modelName, __FUNCTION__, $foreignKey, $localKey);
    }
526218
public function hasReferrer(ForeignKey $fk)
    {
        return $this->hasReferrers() && in_array($fk, $this->referrers, true);
    }
618635
public function forgerForeignKey(AbstractForeignKey $foreignKey)
    {
        foreach ($this->foreignKeys as $name => $foreignSchema) {
            if ($foreignSchema == $foreignKey) {
                unset($this->foreignKeys[$name]);
                break;
            }
        }
    }
499421
final public function addForeignKey(ForeignKey $foreignKey): Column
    {
        if (array_key_exists($foreignKey->getName(), $this->foreignKeys)) {
            throw new InvalidArgumentException('Foreign key '.$foreignKey->getName().' already added.');
        }

        $this->foreignKeys[$foreignKey->getName()] = $foreignKey;

        return $this;
    }
1233335
public function foreignKey($foreignKey, $references = 'id', $unique = false)
    {
        return $this->joinColumn($foreignKey, $references, $unique);
    }
354472
public function getCreateForeignKeySQL(ForeignKeyConstraint $foreignKey, $table)
    {
        if ($table instanceof Table) {
            $table = $table->getQuotedName($this);
        }

        return 'ALTER TABLE ' . $table . ' ADD ' . $this->getForeignKeyDeclarationSQL($foreignKey);
    }
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);
    }
567627
public function setRelatedModel($foreignKey, Model $relatedModel)
    {
        if (count($this->relatedModels) >= 2
            && !array_key_exists($foreignKey, $this->relatedModels)) {

            return;
        }

        $this->relatedModels[$foreignKey] = $relatedModel;
    }
884383
public function belongsToMany($related, $table = null, $foreignKey = null, $otherKey = null, $relation = null)
    {
        if (is_null($relation)) {
            $relation = $this->getBelongsToManyCaller();
        }

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

        $instance = new $related;

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

        if (is_null($table)) {
            $table = $this->joiningTable($related);
        }

        $query = $instance->newQuery();

        return new BelongsToMany($query, $this, $table, $foreignKey, $otherKey, $relation);
    }
1288878
protected function getItem($foreignKey)
    {
        $value = $foreignKey['field'];
        if (! empty($foreignKey['name'])) {
            $value .= "', '". $foreignKey['name'];
        }
        $output = sprintf(
            "\$table->foreign('%s')->references('%s')->on('%s')",
            $value,
            $foreignKey['references'],
            $foreignKey['on']
        );
        if ($foreignKey['onUpdate']) {
            $output .= sprintf("->onUpdate('%s')", $foreignKey['onUpdate']);
        }
        if ($foreignKey['onDelete']) {
            $output .= sprintf("->onDelete('%s')", $foreignKey['onDelete']);
        }
        if (isset($foreignKey['decorators'])) {
            $output .= $this->addDecorators($foreignKey['decorators']);
        }
        return $output . ';';
    }
1296488
public function addForeignKey(ForeignKey $foreignKey)
    {
        $this->table = $this->table->withForeignKeys(
            array_merge($this->table->getForeignKeys(), [$foreignKey])
        );
    }
218341
public function createForeignKey($foreignKey, $table)
    {
        $changeSetElt = $this->createChangeSet('create-foreign-keys-' . $table->getName());
        $addForeignKeyConstraintElt = $this->document->createElement('addForeignKeyConstraint');

        $this->fillForeignKeyAttributes($addForeignKeyConstraintElt, $foreignKey, $table);

        $changeSetElt->appendChild($addForeignKeyConstraintElt);
        $this->root->appendChild($changeSetElt);
    }