There are at least 8 more more closed issues surrounding how to modify the 'data' key that the default serialiser returns. Also a brief search on stackoverflow also confirms this.
see: https://github.com/dingo/api/issues?utf8=%E2%9C%93&q=ArraySerializer
Seems the problem is documentation? Personally cannot find any concise answers on these issues, or on stackoverflow.
Would be great to get a concise explanation with a simple example.
e.g. Most people assume the following from the docs, although it does not work by default:
return $this->response->collection($posts, new PostsTransformer, ['key' => 'posts']);
[
'data' => [
// ...
]
]
[
'posts' => [
// ...
]
]
I am not sure for which key you refer to . For the Collection of for the item.
Check JsonApiSerializer implementation. It's in the package
Basicaly you have to
The answer is simple: You can not change the data key.
You misunderstand the concept of Fractal package that is used on Dingo API to transform data.
This package (Fractal) provides 3 ways to serialize data:
DataArraySerializer: It's name by itself tell us that a data namespace will be added to the output. Whether for items or collections.ArraySerializer: It is for people want to remove, read again REMOVE not change, that data namespace for items, read again items and ONLY items, and that can be done using the ArraySerializer. So collections keep the data namespace to avoid confusing JSON when meta data is added.JsonApiSerializer: This is a representation of the JSON-API standard (v1.0). The standard must be met and it tells that a document must contain at least one of the following top-level members: data, errors, meta. The data is for the document鈥檚 "primary data" and the primary data shortly must be either a single resource object (item) or an array of resource objects (collections). The resource object in turn must have a type and is for specify this type that you use ['key' => 'posts'] like your example where posts in the type of your resource (but it is common to use on singular) on Fractal and consequently on Dingo API.Finally if you want to CHANGE the data key you need to write your own serializer like @catalinux tells you, because Fractal don't let you change data key.
Fortunately this is not so hard as it seems. You can copy the entire DataArraySerializer or JsonApiSerializer implementation whatever you want, and rename it to your own class like PostsSerializer then change the key data for posts. At the end do not forget to set your serializer on the response transformer. If you want to change this key to each resource object, you can check how JsonApiSerializer do this for type and then create a MyResourceSerializer instead of pin the key with posts.
yeah understood
Will see if we can expand the documentation to help clarify this, but the solution proposed above is the right way to go about it. At the most we can direct people to Fractal's docs and suggest making a new transformer, but obviously can't document the entirety of Fractal within our docs.
Yeah, not Dingo's job to document Fractal.
Most helpful comment
The answer is simple: You can not change the
datakey.You misunderstand the concept of Fractal package that is used on Dingo API to transform data.
This package (Fractal) provides 3 ways to serialize data:
DataArraySerializer: It's name by itself tell us that adatanamespace will be added to the output. Whether for items or collections.ArraySerializer: It is for people want to remove, read again REMOVE not change, thatdatanamespace for items, read again items and ONLY items, and that can be done using the ArraySerializer. So collections keep thedatanamespace to avoid confusing JSON when meta data is added.JsonApiSerializer: This is a representation of the JSON-API standard (v1.0). The standard must be met and it tells that a document must contain at least one of the following top-level members:data,errors,meta. Thedatais for the document鈥檚 "primary data" and the primary data shortly must be either a single resource object (item) or an array of resource objects (collections). The resource object in turn must have atypeand is for specify this type that you use['key' => 'posts']like your example wherepostsin thetypeof your resource (but it is common to use on singular) on Fractal and consequently on Dingo API.Finally if you want to CHANGE the
datakey you need to write your own serializer like @catalinux tells you, because Fractal don't let you changedatakey.Fortunately this is not so hard as it seems. You can copy the entire
DataArraySerializerorJsonApiSerializerimplementation whatever you want, and rename it to your own class likePostsSerializerthen change the keydataforposts. At the end do not forget to set your serializer on the response transformer. If you want to change this key to each resource object, you can check howJsonApiSerializerdo this fortypeand then create aMyResourceSerializerinstead of pin the key withposts.