diff --git a/src/Eloquent/HybridRelations.php b/src/Eloquent/HybridRelations.php index bb544f9ae..ee096bc89 100644 --- a/src/Eloquent/HybridRelations.php +++ b/src/Eloquent/HybridRelations.php @@ -29,7 +29,7 @@ trait HybridRelations public function hasOne($related, $foreignKey = null, $localKey = null) { // Check if it is a relation with an original model. - if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) { + if (! is_subclass_of($related, Model::class)) { return parent::hasOne($related, $foreignKey, $localKey); } @@ -55,7 +55,7 @@ public function hasOne($related, $foreignKey = null, $localKey = null) public function morphOne($related, $name, $type = null, $id = null, $localKey = null) { // Check if it is a relation with an original model. - if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) { + if (! is_subclass_of($related, Model::class)) { return parent::morphOne($related, $name, $type, $id, $localKey); } @@ -79,7 +79,7 @@ public function morphOne($related, $name, $type = null, $id = null, $localKey = public function hasMany($related, $foreignKey = null, $localKey = null) { // Check if it is a relation with an original model. - if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) { + if (! is_subclass_of($related, Model::class)) { return parent::hasMany($related, $foreignKey, $localKey); } @@ -105,7 +105,7 @@ public function hasMany($related, $foreignKey = null, $localKey = null) public function morphMany($related, $name, $type = null, $id = null, $localKey = null) { // Check if it is a relation with an original model. - if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) { + if (! is_subclass_of($related, Model::class)) { return parent::morphMany($related, $name, $type, $id, $localKey); } @@ -142,7 +142,7 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat } // Check if it is a relation with an original model. - if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) { + if (! is_subclass_of($related, Model::class)) { return parent::belongsTo($related, $foreignKey, $otherKey, $relation); } @@ -211,13 +211,13 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null /** * Define a many-to-many relationship. * - * @param string $related - * @param string $collection - * @param string $foreignKey - * @param string $otherKey - * @param string $parentKey - * @param string $relatedKey - * @param string $relation + * @param class-string<\Illuminate\Database\Eloquent\Model> $related + * @param string|null $collection + * @param string|null $foreignKey + * @param string|null $otherKey + * @param string|null $parentKey + * @param string|null $relatedKey + * @param string|null $relation * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function belongsToMany( @@ -237,7 +237,7 @@ public function belongsToMany( } // Check if it is a relation with an original model. - if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) { + if (! is_subclass_of($related, Model::class)) { return parent::belongsToMany( $related, $collection, @@ -249,6 +249,7 @@ public function belongsToMany( ); } + // 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. @@ -256,6 +257,10 @@ public function belongsToMany( $instance = new $related; + if ($otherKey === $relation) { + throw new \LogicException(sprintf('In %s::%s(), the key cannot be identical to the relation name "%s". The default key is "%s".', static::class, $relation, $relation, $instance->getForeignKey().'s')); + } + $otherKey = $otherKey ?: $instance->getForeignKey().'s'; // If no table name was provided, we can guess it by concatenating the two @@ -301,7 +306,7 @@ protected function guessBelongsToManyRelation() */ public function newEloquentBuilder($query) { - if (is_subclass_of($this, \Jenssegers\Mongodb\Eloquent\Model::class)) { + if (is_subclass_of($this, Model::class)) { return new Builder($query); } diff --git a/tests/Models/Group.php b/tests/Models/Group.php index 71386fca8..7aa819b45 100644 --- a/tests/Models/Group.php +++ b/tests/Models/Group.php @@ -15,6 +15,6 @@ class Group extends Eloquent public function users(): BelongsToMany { - return $this->belongsToMany(User::class); + return $this->belongsToMany(User::class, 'users', 'groups', 'userIds', '_id', '_id', 'users'); } } diff --git a/tests/Models/User.php b/tests/Models/User.php index 36d6d19ff..1b38d3524 100644 --- a/tests/Models/User.php +++ b/tests/Models/User.php @@ -79,7 +79,12 @@ public function clients() public function groups() { - return $this->belongsToMany(Group::class); + return $this->belongsToMany(Group::class, 'groups', 'users', 'groupIds', '_id', '_id', 'groups'); + } + + public function otherGroups() + { + return $this->belongsToMany(Group::class, 'groups', 'users', 'otherGroups', '_id', '_id', 'groups'); } public function photos() diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index 969fdb0a6..044f30bca 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -344,8 +344,8 @@ public function testBelongsToManyCustom(): void $group = Group::find($group->_id); // Check for custom relation attributes - $this->assertArrayHasKey('user_ids', $group->getAttributes()); - $this->assertArrayHasKey('group_ids', $user->getAttributes()); + $this->assertArrayHasKey('userIds', $group->getAttributes()); + $this->assertArrayHasKey('groupIds', $user->getAttributes()); // Assert they are attached $this->assertContains($group->_id, $user->groups->pluck('_id')->toArray());