Skip to content

Commit ac6f667

Browse files
fix(laravel): collection relations other than HasMany (#6737)
1 parent 195c4e7 commit ac6f667

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/Laravel/Eloquent/Metadata/Factory/Property/EloquentPropertyMetadataFactory.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
use ApiPlatform\Metadata\Exception\PropertyNotFoundException;
1919
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2020
use Illuminate\Database\Eloquent\Model;
21+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
2122
use Illuminate\Database\Eloquent\Relations\HasMany;
23+
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
24+
use Illuminate\Database\Eloquent\Relations\MorphMany;
25+
use Illuminate\Database\Eloquent\Relations\MorphToMany;
2226
use Illuminate\Support\Collection;
2327
use Symfony\Component\PropertyInfo\Type;
2428

@@ -92,10 +96,14 @@ public function create(string $resourceClass, string $property, array $options =
9296
continue;
9397
}
9498

95-
$collection = false;
96-
if (HasMany::class === $relation['type']) {
97-
$collection = true;
98-
}
99+
$collection = match ($relation['type']) {
100+
HasMany::class,
101+
HasManyThrough::class,
102+
BelongsToMany::class,
103+
MorphMany::class,
104+
MorphToMany::class => true,
105+
default => false,
106+
};
99107

100108
$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']));
101109

0 commit comments

Comments
 (0)