Get the foreign key for a model.

@param \Illuminate\Database\Eloquent\Model $model
@return string
187346
protected function getForeignKeyName(Model $model)
    {
        $table = $model->getTable();

        if (array_key_exists($table, $this->foreignKeyLookup)) {
            return $this->foreignKeyLookup[$table];
        }

        return Str::singular($table).'_id';
    }
1117492
public function associate($model)
    {
        $secondOwnerKey = $model instanceof Model ? $model->getAttribute($this->secondOwnerKey) : $model;

        $this->farChild->setAttribute($this->secondKey, $secondOwnerKey);

        if ($model instanceof Model) {
            $this->farChild->setRelation($this->relation, $model);
        }

        return $this->farChild;
    }
736053
public function update(Model $model)
    {
        $metadata = $model->getMetadata();
        $criteria = $this->getQuery()->getRetrieveCritiera($metadata, $model->getId());
        $update = $this->createUpdateObj($model);

        if (empty($update)) {
            return $model;
        }

        $this->getQuery()->executeUpdate($metadata, $model->getStore(), $criteria, $update);
        return $model;
    }
789625
public function saveEntity(Model $model)
    {
        $cacheKey = $this->cacheKey($model->toArray());
        $this->cache->forget($cacheKey);
        return $model->save();
    }
913330
public function getFields($model)
    {
        //
        $table = $this->getTable($model);

        //
        $sql = "DESC `{$table}`";

        //
        $results = $this->getResults($sql);

        //
        $fields = [];

        //
        foreach ($results as $field) {
            $fields[] = $field['Field'];
        }

        //
        return $fields;
    }
350131
public function referencing(Model $model)
    {
        $references = [];

        // TODO: SchemaManager should do this
        foreach ($this->schemas as $schema) {
            $references = array_merge($references, $schema->referencing($model->getBlueprint()));
        }

        // TODO: ModelManager should do this
        foreach ($references as &$related) {
            $blueprint = $related['blueprint'];
            $related['model'] = $model->getBlueprint()->is($blueprint->schema(), $blueprint->table())
                ? $model
                : $this->makeModel($blueprint->schema(), $blueprint->table(), false);
        }

        return $references;
    }
584089
public function get($model, $name)
    {
        $modelName = get_class($model);

        if (!isset($this->_relations[$modelName])) {
            $this->_relations[$modelName] = $model->relations();
            foreach ($this->_relations[$modelName] as $k => $v) {
                if (is_int($k)) {
                    $this->_relations[$modelName][$v] = [];
                    unset($this->_relations[$modelName][$k]);
                }
            }
        }

        if (!isset($this->_relations[$modelName][$name]) || !$this->_relations[$modelName][$name]) {
            /** @noinspection NestedPositiveIfStatementsInspection */
            if ($relation = $this->_inferRelation($model, $name)) {
                $this->_relations[$modelName][$name] = $relation;
            }
        }

        if (isset($this->_relations[$modelName][$name])) {
            $relation = $this->_relations[$modelName][$name];
            if ($relation instanceof Relation) {
                return $relation;
            } else {
                if (is_string($relation)) {
                    $relation = [$relation];
                }

                if (!isset($relation[1])) {
                    $relation[1] = $this->_isPlural($name) ? Relation::TYPE_HAS_MANY : Relation::TYPE_HAS_ONE;
                }
                return $this->_relations[$modelName][$name] = new Relation($model, $relation);
            }
        }

        return false;
    }
722781
protected function hasMany(string $model, ?string $foreignKey = null): HasMany
	{
		$related = new $model;

		return new HasMany($related->getConnection(), $this, $related, $foreignKey);
	}
1134501
public function getForeignKey()
    {
        if (is_null($this->foreignKey)) {
            $name = $this->getParentEntityDescriptor()->getTableName();
            $this->foreignKey = "{$this->normalizeFieldName($name)}_id";
        }
        return $this->foreignKey;
    }
224381
protected function getReference(Model $model)
    {
        $reference = $this->getReferenceValue($model);

        return $this->wrapWithLink($reference, $this->getLinkForReferenceModel($model));
    }
1201351
public function createModel($model)
    {
        $schema = new Schema();

        $tableDef = $schema->createTable($model->getMeta()->getDbTable());
        // this assumes fields set_from_name has been invoked
        $primaryKeyFields = [];
        $unique_fields = [];
        $indexes = [];
        /** @var $field Field */
        /** @var $field ForeignKey */
        foreach ($model->getMeta()->localFields as $fname => $field) {
            $colName = $field->getColumnName();
            $type = $field->dbType($this->connection);
            // if we don't have a type skip
            if (empty($type)) {
                continue;
            }
            if ($field->primaryKey) {
                $primaryKeyFields[] = $model->getMeta()->primaryKey->getColumnName();
            } elseif ($field->isUnique()) {
                $unique_fields[] = $colName;
            } elseif ($field->dbIndex) {
                $indexes[] = $colName;
            }
            $tableDef->addColumn($colName, $type, $this->getDoctrineColumnOptions($field));
            if ($field->isRelation && $field->relation && $field->dbConstraint) {
                $relField = $field->getRelatedField();
                $tableDef->addForeignKeyConstraint(
                    $relField->scopeModel->getMeta()->getDbTable(),
                    [$field->getColumnName()],
                    [$relField->getColumnName()]
                );
            }
        }
        // create the primary key
        $tableDef->setPrimaryKey($primaryKeyFields);
        // add index constraint
        if (!empty($indexes)) {
            $tableDef->addIndex($indexes);
        }
        // add unique constraint
        if (!empty($unique_fields)) {
            $tableDef->addUniqueIndex($unique_fields);
        }
        $this->createTable($tableDef);
        // many to many
        /** @var $relationField ManyToManyField */
        foreach ($model->getMeta()->localManyToMany as $name => $relationField) {
            if ($relationField->manyToMany && $relationField->relation->through->getMeta()->autoCreated) {
                $this->createModel($relationField->relation->through);
            }
        }
    }
1185193
public function getForeignKey()
    {
        if ($this->_foreignKey === null) {
            $this->_foreignKey = $this->_modelKey($this->getSource()->getAlias());
        }

        return $this->_foreignKey;
    }
919139
protected function getRelatedModels(Model $model)
    {
        // Store the class name
        $modelClass = get_class($model);

        // Check if we already know the related models for this model
        if (!isset($this->relatedModels[$modelClass]))
        {
            $relatedModels = [];

            $methods = with(new \ReflectionClass($model))->getMethods();

            // Iterate all class methods
            foreach ($methods as $method)
            {
                // Check if this method returns an Eloquent relation
                if ($method->class == $modelClass &&
                    preg_match('/@return\s+\\\\Illuminate\\\\Database\\\\Eloquent\\\\Relations/', $method->getDocComment())
                )
                {
                    // Get the method name, so that we can call it on the model
                    $relationMethod = $method->name;

                    // Find the relation
                    $relation = $model->$relationMethod();

                    if ($relation instanceof Relation)
                    {
                        // Find the related model
                        $related = $relation->getRelated();

                        // Store the method to help build the inverse path
                        $this->relationClassMethods[$modelClass][get_class($related)] = $relationMethod;

                        $relatedModels[] = ['model' => $related, 'method' => $method];
                    }
                }
            }

            // Cache related models for this model
            $this->relatedModels[$modelClass] = $relatedModels;

            // Return the related models
            return $relatedModels;
        } else
        {
            // Return from cache
            return $this->relatedModels[$modelClass];
        }
    }
182674
protected function getForeignKey()
	{
		if($this->foreignKey === null)
		{
			$this->foreignKey = $this->parent->getForeignKey();
		}

		return $this->foreignKey;
	}
793767
public function isOwner(Model $Model) {
		if (!$Model->exists()) {
			return false;
		}

		if (!$owner = Hash::get($Model->data, $Model->alias . '.' . $this->settings[$Model->alias]['owner']['path'])) {
			$requireAuth = $Model->requireAuth;
			$Model->requireAuth = false;
			$owner = $Model->field(
				$this->settings[$Model->alias]['owner']['path'],
				array($Model->alias . '.' . $Model->primaryKey => $Model->getID())
			);
			$Model->requireAuth = $requireAuth;
		}

		return $this->getCurrentUser($Model, $this->settings[$Model->alias]['owner']['auth']) == $owner;
	}
120372
public function defaultModel($model = null)
    {
        if ($model !== null) {
            $this->_defaultModel = $model;
        }
        if ($this->_defaultModel) {
            return $this->_defaultModel;
        }
        if (!$this->_View->getRequest()->getParam('paging')) {
            return null;
        }
        list($this->_defaultModel) = array_keys($this->_View->getRequest()->getParam('paging'));

        return $this->_defaultModel;
    }
31683
protected function getDefaultFor(Model $model)
    {
        if (! $this->withDefault) {
            return;
        }

        $instance = $this->related->newInstance()->setAttribute(
            $this->getForeignKeyName(),
            $model->getAttribute($this->localKey)
        );

        if (is_callable($this->withDefault)) {
            return call_user_func($this->withDefault, $instance) ?: $instance;
        }

        if (is_array($this->withDefault)) {
            $instance->forceFill($this->withDefault);
        }

        return $instance;
    }