laravel-json-api / eloquent

Serialize Eloquent models to JSON API resources
MIT License
12 stars 15 forks source link

[Feature] Add polymorphic to-many relation #7

Closed lindyhopchris closed 3 years ago

lindyhopchris commented 3 years ago

Adds a MorphToMany relation field, that effectively wraps multiple relation fields, for example:

MorphToMany::make('media', [
    BelongsToMany::make('images'),
    BelongsToMany::make('videos'),
]),

This relatonship aggregates the results from each of its sub-relations, with the value returned in either the data member of its relationship field, or in a relationship endpoint.

The field is writeable as long as:

  1. every sub-relationship is writeable (implements FillableToMany); and
  2. each resource type that can be in the relationships maps to a single sub-relation.

Include paths also work, with the include paths only being applied to the sub-relations for which the include path is valid.