Skip to content

fix HybridRelations::belongsToMany not compatible with Model::belongsToMany #1282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/Jenssegers/Mongodb/Eloquent/HybridRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,20 @@ public function morphTo($name = null, $type = null, $id = null)
}
}

/**
**

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

miss the slash.

* Define a many-to-many relationship.
*
* @param string $related
* @param string $collection
* @param string $foreignKey
* @param string $otherKey
* @param string $foreignPivotKey
* @param string $relatedPivotKey
* @param string $parentKey
* @param string $relatedKey
* @param string $relation
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null, $relation = null)
public function belongsToMany($related, $collection = null, $foreignPivotKey = null, $relatedPivotKey = null,
$parentKey = 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
Expand All @@ -225,17 +228,18 @@ public function belongsToMany($related, $collection = null, $foreignKey = null,

// Check if it is a relation with an original model.
if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $relation);
return parent::belongsToMany($related, $collection = null, $foreignPivotKey = null, $relatedPivotKey = null,
$parentKey = null, $relatedKey = null, $relation = null);
}

// 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.
$foreignKey = $foreignKey ?: $this->getForeignKey().'s';
$foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey().'s';

$instance = new $related;

$otherKey = $otherKey ?: $instance->getForeignKey().'s';
$relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey().'s';

// If no table name was provided, we can guess it by concatenating the two
// models using underscores in alphabetical order. The two model names
Expand All @@ -249,7 +253,9 @@ public function belongsToMany($related, $collection = null, $foreignKey = null,
// appropriate query constraint and entirely manages the hydrations.
$query = $instance->newQuery();

return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $relation);
return new BelongsToMany($query, $this, $collection,
$foreignPivotKey, $relatedPivotKey,
$parentKey, $relatedKey, $relation);
}

/**
Expand Down