laravel-responder icon indicating copy to clipboard operation
laravel-responder copied to clipboard

Polymorphic relation mapping

Open IlCallo opened this issue 7 years ago β€’ 15 comments

Derived by #84 discussion. The idea is to allow eager loading (via the mapping implemented in 3.0 on both $relations and $load) also on morphable fields.

The syntax would be something like

$relation = [
  'morphableRelation' => [
    PizzaTransformer::class,
    MafiaTransformer::class,
    MandolinoTransformer::class,
  ]
]

I'm not sure about how it's possible to implement this without knowing the underlying data or having access to the models, but I think that using a full join maybe can be useful to retrieve all different relations of various morphed records.

Or we can allow a more complex definition and then fallback to meaningful defaults as Laravel does

$relation = [
  'morphableRelation' => [
    'pizzas' => PizzaTransformer::class,
    'mafias' => MafiaTransformer::class,
    'mandolins' => MandolinoTransformer::class,
  ]
]

In this way it's possible to add constraints on the eager loading query and load PizzaTransformer relations only when morphable_type is equal to "pizzas" (I guess the relation contains the field name of the related morphable and can be retrieved there) and same goes for the other entries. If indexes are not given (so like the first example), the transformer class itself is used to check against morphable_type (because Laravel by default uses the fully qualified names in this cases), unless a morphable map is defined (check here) and contains a mapping for the Model associated to the transformer, which can usually be retrieved by the Transformer name (App\Transformers\PizzaTransformer => Pizza).

Do you think this can be done?

IlCallo avatar Feb 26 '18 15:02 IlCallo

I'll tinker some with polymorphic relations when I have time and see how possible something like this would be to implement.

flugg avatar Feb 27 '18 09:02 flugg

Thank you :)

IlCallo avatar Feb 27 '18 14:02 IlCallo

+1 :)

Hesesses avatar Apr 06 '18 12:04 Hesesses

any ideas / updates on this?

Hesesses avatar Oct 30 '18 13:10 Hesesses

Not yet, unfortunately :(

Have had limited time to work on open source the last months, but should have some time to work on this and other package-related stuff over Christmas!

flugg avatar Nov 02 '18 07:11 flugg

Sorry to be pain in the ass, but any updates on this? πŸ™

Hesesses avatar Jan 26 '19 14:01 Hesesses

You’re not a pain in the ass πŸ˜„ just been busy with work lately, but will make sure to put this on the top of the priority list!

flugg avatar Jan 29 '19 07:01 flugg

Hey, gentle bump on this feature, how's it coming along?

ld-gary avatar Aug 05 '19 11:08 ld-gary

πŸ˜‡ πŸ™

Hesesses avatar Sep 06 '19 14:09 Hesesses

Sorry for this taking a long time - I'm almost done with a new major version of the package which swaps out Fractal for API resources. I'll ensure the new version has a good solution for polymorphic relations!

flugg avatar Nov 22 '19 17:11 flugg

Any updates with the new major version? πŸ™

Hesesses avatar Feb 28 '20 09:02 Hesesses

It's technically finished, just missing some tests and documentation!

The more I've worked on it the more I've realised I want to keep the feature set at a minimum. Currently I've replaced Fractal's Serializers with a new Formatter concept. However, since Transformers are replaced with Laravel's API Resources I've removed things like with and only since they would require changes to the resource classes themselves which I feel goes outside the scope of the package. That means this issue is no longer part of the responsibilities of this package. Instead I'm working on a new package which handles requests and can do filtering, inclusion of relations and sorting.

I've held back a bit on the major version because I didn't really have a good replacement for the features which are getting removed. With that being said I feel like the new version is a big improvement from the current version and even though it does a bit less things than before it does these things better.

I can push the new version up to a new branch if you (or anybody else) want to check it out and provide feedback?

flugg avatar Feb 28 '20 13:02 flugg

Would be nice to be able test if and provide some feedback also!

For me (and also our projects) the ?with=xxx feature has been one of the most used features

Hesesses avatar Mar 08 '20 21:03 Hesesses

Yeah, I feel you and I've used this feature a lot myself. Basically, I would like to have possibilities to not only include relations and filter fields with query parameters, but also query a list of data, sort the data and more. If I were to include all of these features to this package I feel the scope would be very broad and the documentation a bit too heavy. And if I were to only keep the inclusion of relations and filtering fields for this package then there would be two packages concerned about parsing the query string which I would like to avoid.

Either ways, I'll think some more about it - I might re-add these features until the new package is released. The new version is released on a new branch now, would love your feedback: https://github.com/flugger/laravel-responder/tree/feature/api-resources (keep in mind the documentation is not done).

flugg avatar Mar 09 '20 09:03 flugg

I have managed to create a polymorphic fractal transformer. Have a look at https://github.com/yucadoo/polymorphic-fractal

Firtzberg avatar Apr 16 '20 10:04 Firtzberg