当前位置: 动力学知识库 > 问答 > 编程问答 >

api - PHP Fractal transformers - Multiple Transformers for a single model?

问题描述:

Is it better to have multiple Transformers over a single transformer per model?

e.g.

I want to exclude a fields from the JSON response when listing items. Although when retrieving a single item I want all fields. Would a PostListTransformer and a PostTransformer be appropriate, or should i use the "includes" feature of Fractal?

Also I would like the ability to include extra fields AND relations in the PostListTransformer.

Examples will be well regarded, Laravel Eloquent is the ORM.

网友答案:

What you could do is add a $fields property that gets populated with the fields you only need through your transformer's constructor method, then create a method that uses those fields (if any exist) to filter out your transform() method output. You can find some code for it in one of the closed issues in the fractal github repo

https://github.com/thephpleague/fractal/issues/226#issuecomment-156802672

分享给朋友:
您可能感兴趣的文章:
随机阅读: