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
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
],
"license" : "MIT",
"require": {
"illuminate/support": "^5.1",
"illuminate/container": "^5.1",
"illuminate/database": "^5.1",
"illuminate/events": "^5.1",
"illuminate/support": "^5.5",
"illuminate/container": "^5.5",
"illuminate/database": "^5.5",
"illuminate/events": "^5.5",
"mongodb/mongodb": "^1.0.0"
},
"require-dev": {
Expand Down
20 changes: 13 additions & 7 deletions src/Jenssegers/Mongodb/Eloquent/HybridRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,15 @@ public function morphTo($name = null, $type = null, $id = null)
*
* @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