From b1aef84b54d19a5244dfc744a2d6802c6b95c83b Mon Sep 17 00:00:00 2001 From: Vincent Girol Date: Sat, 13 May 2023 22:50:18 +0200 Subject: [PATCH] Fix error when a method return type is union --- src/Concerns/InteractsWithRelationships.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Concerns/InteractsWithRelationships.php b/src/Concerns/InteractsWithRelationships.php index a06dcc7..1bb2494 100644 --- a/src/Concerns/InteractsWithRelationships.php +++ b/src/Concerns/InteractsWithRelationships.php @@ -12,6 +12,7 @@ use ReflectionClass; use ReflectionMethod; +use ReflectionUnionType; trait InteractsWithRelationships { @@ -67,8 +68,15 @@ protected function relationshipsViaType(Model|Null $model) { } $returnType = $method->getReturnType(); - if (!is_null($returnType) && $returnType->getName() === BelongsTo::class) { - return $method->getShortName() ?? null; + if ($returnType instanceof ReflectionUnionType) { + $returnTypes = $returnType->getTypes(); + } else { + $returnTypes = [$returnType]; + } + foreach ($returnTypes as $type) { + if (!is_null($type) && $type->getName() === BelongsTo::class) { + return $method->getShortName() ?? null; + } } }