From ac6f667f301f6c4c399a707faf00567239bd98d8 Mon Sep 17 00:00:00 2001 From: Amer Chaudhary Date: Wed, 23 Oct 2024 17:29:25 +0500 Subject: [PATCH] fix(laravel): collection relations other than HasMany (#6737) --- .../Property/EloquentPropertyMetadataFactory.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyMetadataFactory.php b/src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyMetadataFactory.php index b0aee59ee8f..c88c96585e2 100644 --- a/src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyMetadataFactory.php +++ b/src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyMetadataFactory.php @@ -18,7 +18,11 @@ use ApiPlatform\Metadata\Exception\PropertyNotFoundException; use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\HasManyThrough; +use Illuminate\Database\Eloquent\Relations\MorphMany; +use Illuminate\Database\Eloquent\Relations\MorphToMany; use Illuminate\Support\Collection; use Symfony\Component\PropertyInfo\Type; @@ -92,10 +96,14 @@ public function create(string $resourceClass, string $property, array $options = continue; } - $collection = false; - if (HasMany::class === $relation['type']) { - $collection = true; - } + $collection = match ($relation['type']) { + HasMany::class, + HasManyThrough::class, + BelongsToMany::class, + MorphMany::class, + MorphToMany::class => true, + default => false, + }; $type = new Type($collection ? Type::BUILTIN_TYPE_ITERABLE : Type::BUILTIN_TYPE_OBJECT, false, $relation['related'], $collection, collectionValueType: new Type(Type::BUILTIN_TYPE_OBJECT, false, $relation['related']));