-
Notifications
You must be signed in to change notification settings - Fork 46
Description
This has long been requested for Laravel JSON:API. Part of doing the revamp was to make things like this easier to implement. Background here: cloudcreativity/laravel-json-api#166
The idea is basically to give the client control over what relationships have a count field added to their meta.
https://laravel.com/docs/8.x/eloquent-relationships#counting-related-models
Something like:
GET /api/v1/posts?with-count=comments
Would return the posts resources with a count added to the meta of the comments relationship.
For relationship endpoints, we'd need to distinquish between counting the relation that is being retrieved, and adding a count to the models within the relation. E.g:
GET /api/v1/posts/1/comments?with-count=likes
In this request, the likes refers to a relationship on the comments resource. But we may want to include the count of how many comments the post has - so we need another parameter for indicating that for relationship endpoints. E.g. ?count-related.
Note that the JSON:API spec recommends using at least one non a-z character in the query parameter name, to avoid potential collisions with future additions by the spec. So
with-count,with_countandwithCountwould all be ok. (To follow our convention,with-countshould be the default but should be configurable.)
Once we've implemented for count, it presumably will be easy to implement the other aggregate functions.