Illuminate\Database\Eloquent\Model::morphToMany PHP Method

morphToMany() public method

Define a polymorphic many-to-many relationship.
public morphToMany ( string $related, string $name, string $table = null, string $foreignKey = null, string $otherKey = null, boolean $inverse = false ) : Illuminate\Database\Eloquent\Relations\MorphToMany
$related string
$name string
$table string
$foreignKey string
$otherKey string
$inverse boolean
return Illuminate\Database\Eloquent\Relations\MorphToMany
    public function morphToMany($related, $name, $table = null, $foreignKey = null, $otherKey = null, $inverse = false)
    {
        $caller = $this->getBelongsToManyCaller();
        // First, we will need to determine the foreign key and "other key" for the
        // relationship. Once we have determined the keys we will make the query
        // instances, as well as the relationship instances we need for these.
        $foreignKey = $foreignKey ?: $name . '_id';
        $instance = new $related();
        if (!$instance->getConnectionName()) {
            $instance->setConnection($this->connection);
        }
        $otherKey = $otherKey ?: $instance->getForeignKey();
        // Now we're ready to create a new query builder for this related model and
        // the relationship instances for this relation. This relations will set
        // appropriate query constraints then entirely manages the hydrations.
        $query = $instance->newQuery();
        $table = $table ?: Str::plural($name);
        return new MorphToMany($query, $this, $name, $table, $foreignKey, $otherKey, $caller, $inverse);
    }
Model