Check if related model actually owns the relationship.

@param  \Illuminate\Database\Eloquent\Model|null  $related
@param  string|null  $foreignKey

@return bool
548388
public function owns(Model $related = null, ?string $foreignKey = null): bool
    {
        if (\is_null($related)) {
            return false;
        }

        if (\is_null($foreignKey)) {
            $foreignKey = $this->getForeignKey();
        }

        return $related->getAttribute($foreignKey) == $this->getKey();
    }
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);
    }
754301
protected function hasOneOrManyDeepForeignKeys(Model $related, array $throughParents, array $foreignKeys)
    {
        foreach (array_merge([$this], $throughParents) as $i => $instance) {
            /** @var \Illuminate\Database\Eloquent\Model $instance */
            if (! isset($foreignKeys[$i])) {
                if ($instance instanceof Pivot) {
                    $foreignKeys[$i] = ($throughParents[$i] ?? $related)->getKeyName();
                } else {
                    $foreignKeys[$i] = $instance->getForeignKey();
                }
            }
        }

        return $foreignKeys;
    }
1241537
protected function has_many( $model, $foreign_key = null, $local_key = null )
	{
		return new Model_Relation_HasMany( $this, $model, $foreign_key, $local_key );
	}
1290803
protected function checkForeignKeyConstraint(ForeignKeyConstraint $foreignKeyConstraint, IgnoredChange $ignoredChange)
    {
        // Not needed to be implemented yet
        if ($ignoredChange->getPropertyName() !== $foreignKeyConstraint->getName()) {
            return false;
        }

        return false;
    }
94954
public function addRelationship($id, $relationship, $related_id, $type = false)
    {
        foreach ($this->data as $key => $data) {
            if ($data->getId() == $id) {
                $this->data[$key]->addRelationship($relationship, $related_id, $type);
                return true;
            }
        }
        return false;
    }
1224827
protected function hasOne(string $model, ?string $foreignKey = null): HasOne
	{
		$related = new $model;

		return new HasOne($related->getConnection(), $this, $related, $foreignKey);
	}
94324
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(false, 2);

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

        $specialType = $this->getCmsSpecialRelationType($relation);

        // 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)) {
            if ($specialType === self::RELATION_TYPE_CATEGORY) {
                $foreignKey = config('pxlcms.relations.categories.keys.category');
            } else {
                $foreignKey = Str::snake($relation);
            }
        }

        // If the relation name is the same (casing aside) as the foreign key attribute,
        // we must make sure that the foreign key property is at least a key in the
        // attributes array. If it is not, there will be recursive lookup problems
        // where the attribute is never resolved and 'property does not exist' exceptions occur.
        if (    snake_case($relation) == $foreignKey
            &&  ! array_key_exists($foreignKey, $this->attributes)
        ) {
            $this->attributes[ $foreignKey ] = null;
        }

        /** @var Builder $belongsTo */
        $belongsTo = parent::belongsTo($related, $foreignKey, $otherKey, $relation);

        // category relation must be filtered by module id
        if ($specialType === self::RELATION_TYPE_CATEGORY) {
            $belongsTo->where(
                config('pxlcms.relations.categories.keys.module'),
                $this->getModuleNumber()
            );
        }

        return $belongsTo;
    }
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;
    }
954969
public function hasOne($related, $foreignKey = null, $localKey = null)
    {
        if (is_array($foreignKey)) { //Check for multi-columns relationship
            $this->validateRelatedModel($related);
        }

        $instance = $this->newRelatedInstance($related);

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

        $foreignKeys = null;

        if (is_array($foreignKey)) { //Check for multi-columns relationship
            foreach ($foreignKey as $key) {
                $foreignKeys[] = $this->sanitizeKey($instance, $key);
            }
        } else {
            $foreignKey = $this->sanitizeKey($instance, $foreignKey);
        }

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

        return new HasOne($instance->newQuery(), $this, $foreignKeys ?: $foreignKey, $localKey);
    }
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));
    }
1194526
protected function _relationship($modelName, $relationship, $foreignKey=null, $localKey=null)
    {
        /**
         * PSR-4 support check
         * 
         * @see https://github.com/yidas/codeigniter-psr4-autoload
         */
        if (strpos($modelName, "\\") !== false ) {
            
            $model = new $modelName;

        } else {
            // Original CodeIgniter 3 model loader
            get_instance()->load->model($modelName);
            $model = $this->$modelName;
        }

        $libClass = __CLASS__;
        
        // Check if is using same library
        if (!is_subclass_of($model, $libClass)) {
            throw new Exception("Model `{$modelName}` does not extend {$libClass}", 500);
        }

        // Keys
        $foreignKey = ($foreignKey) ? $foreignKey : $this->primaryKey;
        $localKey = ($localKey) ? $localKey : $this->primaryKey; 

        $query = $model->find()
            ->where($foreignKey, $this->$localKey);

        // Inject Model name into query builder for ORM relationships
        $query->modelName = $modelName;
        // Inject relationship type into query builder for ORM relationships
        $query->relationship = $relationship;

        return $query;
    }
619339
protected function isBelongsToMany($foreignKey)
    {
        $remote = $this->describes[$foreignKey->TABLE_NAME];
        $count = 0;
        foreach ($remote as $field) {
            if ($field->Key == 'PRI') {
                $count++;
            }
        }
        if ($count == 2) {
            return true;
        }

        return false;
    }
877313
public function hasOne($related, $parentKey = null, $localKey = null)
    {
        $parentKey = $parentKey ?: $this->getParentKey();
        $localKey = $localKey ?: $this->getKeyName();

        $instance = new $related;

        $relation = new Relations\Relation($instance, $this, $parentKey, $localKey);

        return $relation->find($localKey);
    }
268945
public function belongsToMany(string $related, ?string $primary_key = null, ?string $foreign_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 BelongsToMany($relatedModel, $this, $primary_key, $foreign_key);
    }
141834
protected function relationColumn($related, $model, $column = null)
    {
        $table = $related::$table;
        //
        return !is_null($column) ? $column : $this->idKey($table);
    }
456463
public function scopeOwnedBy(Builder $query, Model $related, ?string $key = null): Builder
    {
        if (\is_null($key)) {
            $key = $related->getForeignKey();
        }

        return $query->where($key, $related->getKey());
    }