Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Aug 30, 2023
1 parent c105da6 commit da4339c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
33 changes: 19 additions & 14 deletions src/Eloquent/HybridRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -249,13 +249,18 @@ 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.
$foreignKey = $foreignKey ?: $this->getForeignKey().'s';

$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
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Models/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
7 changes: 6 additions & 1 deletion tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions tests/RelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit da4339c

Please sign in to comment.