@todo: Not correctly implemented.

An Entity can reference many other Entity(s).

@return Illuminate\Database\Eloquent\Relations\belongsToMany
542485
public function references($type)
    {
		// Build and return relation.
		return $this->belongsToMany(Config::get('sorad.entity.aggregates.' . $this->toKebabCase($type)), 'entity_reference', 'entity_id', 'reference_id')
					->withPivot('order')
					->where('entity_type', '=', $this->getType())
					->where('reference_type', '=', $type)
					->whereNull('entity_reference.deleted_at')
					->orderBy('order', 'ASC');
	}
370556
public function getRelationships()
    {
        $relationships = array();

        foreach ($this->getProperty() as $name => $property) {
            if ($property->isRelationship()) {
                $relationships[$name] = $property;
            }
        }

        return $relationships;
    }
725422
public function manyToMany ($relatedTable, $relationalTable, $relatedKey = null, $key = null, $relationIdentifier = null)
		{
			if (is_null($relationIdentifier)) {
				$relationIdentifier = $relatedTable;
			}

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

			return $this;
		}
548392
public function counterpart()
    {
        if ($this->_counterpart) {
            return $this->_counterpart;
        }

        $to = $this->to();

        $from = $this->from();
        $relations = $to::definition()->relations();
        $result = [];

        foreach ($relations as $relation) {
            $rel = $to::definition()->relation($relation);
            if ($rel->to() === $this->from()) {
                $result[] = $rel;
            }
        }
        if (count($result) === 1) {
            return $this->_counterpart = reset($result);
        } elseif (count($result) > 1) {
            throw new ORMException("Ambiguous {$this->type()} counterpart relationship for `{$from}`. Apply the Single Table Inheritance pattern to get unique models.");
        }
        throw new ORMException("Missing {$this->type()} counterpart relationship for `{$from}`. Add one in the `{$to}` model.");
    }
18596
public function getSymmetricalRelation()
    {
        $localMapping = [$this->getLeftColumns(), $this->getRightColumns()];
        foreach ($this->getRightTable()->getRelations() as $relation) {
            if ($localMapping == [$relation->getRightColumns(), $relation->getLeftColumns()]) {
                return $relation;
            }
        }
    }
38461
public function belongsToThrough($related, $through, $firstKey = null, $secondKey = null, $localKey = null)
    {
        $through = new $through;

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

        $secondKey = $secondKey ?: $through->getForeignKey();

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

        return new BelongsToThrough((new $related)->newQuery(), $this, $through, $firstKey, $secondKey, $localKey);
    }
940553
public function roles()
    {
        $roleModel = config('defender.role_model', 'Artesaos\Defender\Role');
        $roleUserTable = config('defender.role_user_table', 'role_user');
        $roleKey = config('defender.role_key', 'role_id');

        return $this->belongsToMany($roleModel, $roleUserTable, 'user_id', $roleKey);
    }
92589
public function permissions()
    {
        return $this->belongsToMany(
            Config::get('laratrust.models.permission'),
            Config::get('laratrust.tables.permission_role'),
            Config::get('laratrust.foreign_keys.role'),
            Config::get('laratrust.foreign_keys.permission')
        );
    }
639427
protected function eagerLoadRelations()
    {
        $query = $this->model;

        if (is_array($this->eagerLoadRelations)) {
            $eagerLoads = [];

            foreach ($this->eagerLoadRelations as $relation => $rules) {
                // case for relations[]=relation_name
                // with relation that is numeric
                $relation = (is_integer($relation)) ? $rules : $relation;

                // case for relations[relation_name][attributes][]=attrib_name
                // with relation that is a relation name
                if (is_array($rules) && array_key_exists('attributes', $rules)) {
                    $eagerLoads[$relation] = function ($q) use ($rules) {
                        // If query is instance of HasOneOrMany,
                        // make sure to select the parent key name as foreign key.
                        if (
                            $q instanceof HasOneOrMany &&
                            !in_array($fk = $q->getParent()->getForeignKey(), $rules['attributes'])
                        ) {
                            array_push($rules['attributes'], $fk);
                        }

                        $q->select($rules['attributes']);
                    };
                } else {
                    array_push($eagerLoads, $relation);
                }
            }

            return $query->with($eagerLoads);
        }

        return $query->with($this->eagerLoadRelations);
    }
1135358
public function permissions() : BelongsToMany
    {
        $pivotTable = config('lia.database.user_permissions_table');

        $relatedModel = config('lia.database.permissions_model');

        return $this->belongsToMany($relatedModel, $pivotTable, 'user_id', 'permission_id');
    }
455105
public function getReferences() {
		if (count($this->references) == 0) {
			$this->references = EntityReflexion::getReferences(get_class($this));
		}
		return $this->references;
	}
258444
public function belongsTo($belongsTo = null)
    {
        if ($belongsTo === null)
        {
            return $this->_belongsTo;
        }
        $this->_belongsTo = $belongsTo;

        return $this;
    }
372420
public function roles()
    {
        $relation = $this->morphToMany(
            Models::classname(Role::class),
            'entity',
            Models::table('assigned_roles')
        )->withPivot('scope');

        return Models::scope()->applyToRelation($relation);
    }
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
        );
    }
44728
public function users()
    {
        return $this
            ->belongsToMany(
                config('laravel-auth.users.model', User::class),
                $this->getPrefix().config('laravel-auth.role-user.table', 'permission_role')
            )
            ->using(Pivots\RoleUser::class)
            ->withTimestamps();
    }
72453
public function belongsTo($entity, string $related, string $foreignKey = null, string $otherKey = null): BelongsTo
    {
        // Add the relation to the definition in map
        list(, $caller) = debug_backtrace(false);
        $relation = $caller['function'];
        $this->relatedClasses[$relation] = $related;

        $this->addSingleRelation($relation);
        $this->addLocalRelation($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)) {
            $foreignKey = snake_case($relation).'_id';
        }

        $this->localForeignKeys[$relation] = $foreignKey;

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

        $otherKey = $otherKey ?: $relatedMapper->getEntityMap()->getKeyName();

        return new BelongsTo($relatedMapper, $entity, $foreignKey, $otherKey, $relation);
    }
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);
    }