Skip to content

Commit

Permalink
fix(laravel): collection relations other than HasMany (#6737)
Browse files Browse the repository at this point in the history
  • Loading branch information
amermchaudhary authored Oct 23, 2024
1 parent 195c4e7 commit ac6f667
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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']));

Expand Down

0 comments on commit ac6f667

Please sign in to comment.