diff --git a/src/Database/Concerns/HasRelationships.php b/src/Database/Concerns/HasRelationships.php index 60b1ee31e..c1b0783d4 100644 --- a/src/Database/Concerns/HasRelationships.php +++ b/src/Database/Concerns/HasRelationships.php @@ -792,7 +792,12 @@ protected function getRelationMethodType(string $name, bool $ignoreResolved = fa $object = $this->extensionData['extensions'][$extension]; $method = new \ReflectionMethod($object, $name); } elseif (isset($this->extensionData['dynamicMethods'][$name])) { - $method = new \ReflectionFunction($this->extensionData['dynamicMethods'][$name]->getClosure()); + $dynamicMethod = $this->extensionData['dynamicMethods'][$name]; + if (is_array($dynamicMethod)) { + $method = new \ReflectionMethod(...$dynamicMethod); + } else { + $method = new \ReflectionFunction($dynamicMethod->getClosure()); + } } else { if (!isset(static::$resolvedNonRelationMethods[static::class])) { static::$resolvedNonRelationMethods[static::class] = [$name]; diff --git a/tests/Database/Concerns/HasRelationshipsTest.php b/tests/Database/Concerns/HasRelationshipsTest.php index 8e7fa9aef..941eefbda 100644 --- a/tests/Database/Concerns/HasRelationshipsTest.php +++ b/tests/Database/Concerns/HasRelationshipsTest.php @@ -48,6 +48,30 @@ public function testHasRelation() $this->assertFalse($author->hasRelation('invalid')); } + public function testRelationDynamicMethods() + { + $author = new Author(); + + $author->addDynamicMethod('dynamicClassMethodFromArray', [$author, 'messages']); + $this->assertTrue($author->hasRelation('dynamicClassMethodFromArray')); + $this->assertEquals('hasMany', $author->getRelationType('dynamicClassMethodFromArray')); + } + + public function testNonRelationDynamicMethods() + { + $author = new Author(); + + $author->addDynamicMethod('dynamicMethodAsClosure', function () { + }); + $this->assertFalse($author->hasRelation('dynamicMethodAsClosure')); + + $author->addDynamicMethod('dynamicStaticClassMethodFromArray', [Author::class, 'hasDatabaseTable']); + $this->assertFalse($author->hasRelation('dynamicStaticClassMethodFromArray')); + + $author->addDynamicMethod('dynamicClassMethodFromArray', [$author, 'isDatabaseReady']); + $this->assertFalse($author->hasRelation('dynamicClassMethodFromArray')); + } + public function testGetRelationType() { $author = new Author();