Skip to content

fix(laravel): skip relation metadata for abstract Eloquent models#7933

Open
cay89 wants to merge 1 commit intoapi-platform:4.3from
cay89:fix/7911-laravel-abstract-model
Open

fix(laravel): skip relation metadata for abstract Eloquent models#7933
cay89 wants to merge 1 commit intoapi-platform:4.3from
cay89:fix/7911-laravel-abstract-model

Conversation

@cay89
Copy link
Copy Markdown
Contributor

@cay89 cay89 commented Apr 28, 2026

Q A
Branch? 4.3
Tickets Closes #7911
License MIT
Doc PR n/a

RelationMetadataLoader crashes with Cannot instantiate abstract class when an API resource model extends an abstract base model.

The Symfony serializer's ClassMetadataFactory walks the full class hierarchy when building metadata. For a chain like ItemTranslation extends AbstractModel extends Model, the loader is also invoked for AbstractModel. Since AbstractModel is still a subclass of Model, the existing is_a(..., Model::class, true) guard passes, and $refl->newInstanceWithoutConstructor() then throws because the class is abstract.

This PR adds an early return false when the reflected class is abstract — there is nothing meaningful to load on an abstract model anyway, and the recursion continues normally for the concrete subclass.

Changes

  • src/Laravel/Eloquent/Serializer/Mapping/Loader/RelationMetadataLoader.php: guard with ReflectionClass::isAbstract() before instantiation.
  • New regression test: src/Laravel/Tests/Eloquent/Serializer/Mapping/Loader/RelationMetadataLoaderTest.php.
  • New workbench fixture: src/Laravel/workbench/app/Models/AbstractModel.php (generic abstract Eloquent model, reusable by future tests).

Backwards compatibility

No BC break: previously, calling the loader with an abstract model class always threw Error: Cannot instantiate abstract class. There is no existing code path that relied on this behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant