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 81a0fc6 commit b4d3dc1
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/Eloquent/EmbedsRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
trait EmbedsRelations
{
/**
* @var array{class-string<self>, bool}
* @var array<class-string, array<string, bool>>
*/
private static array $embeddedCache = [];
private static array $hasEmbeddedRelation = [];

/**
* Define an embedded one-to-many relationship.
Expand Down Expand Up @@ -98,13 +98,13 @@ private function hasEmbeddedRelation(string $key): bool
return false;
}

if (isset(self::$embeddedCache[static::class][$key])) {
return self::$embeddedCache[static::class][$key];
if (isset(self::$hasEmbeddedRelation[static::class][$key])) {
return self::$hasEmbeddedRelation[static::class][$key];
}

$returnType = (new ReflectionMethod($this, $method))->getReturnType();

return self::$embeddedCache[static::class][$key] = $returnType instanceof ReflectionNamedType
return self::$hasEmbeddedRelation[static::class][$key] = $returnType instanceof ReflectionNamedType
&& in_array($returnType->getName(), [EmbedsOne::class, EmbedsMany::class], true);
}
}
4 changes: 0 additions & 4 deletions src/Eloquent/HybridRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,6 @@ 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
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, 'users', 'groups', 'userIds', '_id', '_id', 'users');
return $this->belongsToMany(User::class, 'users', 'groupIds', 'userIds', '_id', '_id', 'users');
}
}
7 changes: 1 addition & 6 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,7 @@ public function clients()

public function groups()
{
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');
return $this->belongsToMany(Group::class, 'groups', 'userIds', 'groupIds', '_id', '_id', 'groups');
}

public function photos()
Expand Down

0 comments on commit b4d3dc1

Please sign in to comment.