From b4d3dc177be376999dc00d253bba6c4a8f95be5c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= <jerome@tamarelle.net>
Date: Wed, 30 Aug 2023 16:12:07 +0200
Subject: [PATCH] -

---
 src/Eloquent/EmbedsRelations.php | 10 +++++-----
 src/Eloquent/HybridRelations.php |  4 ----
 tests/Models/Group.php           |  2 +-
 tests/Models/User.php            |  7 +------
 4 files changed, 7 insertions(+), 16 deletions(-)

diff --git a/src/Eloquent/EmbedsRelations.php b/src/Eloquent/EmbedsRelations.php
index 97cbdfc58..ebc18b6ed 100644
--- a/src/Eloquent/EmbedsRelations.php
+++ b/src/Eloquent/EmbedsRelations.php
@@ -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.
@@ -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);
     }
 }
diff --git a/src/Eloquent/HybridRelations.php b/src/Eloquent/HybridRelations.php
index 8e42903c0..e2270f218 100644
--- a/src/Eloquent/HybridRelations.php
+++ b/src/Eloquent/HybridRelations.php
@@ -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
diff --git a/tests/Models/Group.php b/tests/Models/Group.php
index 1cad278c6..ad079e6b9 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, 'users', 'groups', 'userIds', '_id', '_id', 'users');
+        return $this->belongsToMany(User::class, 'users', 'groupIds', 'userIds', '_id', '_id', 'users');
     }
 }
diff --git a/tests/Models/User.php b/tests/Models/User.php
index 26f57e9ba..77d184f76 100644
--- a/tests/Models/User.php
+++ b/tests/Models/User.php
@@ -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()