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; + } } }