-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Counting relationships? #166
Comments
To be clear, I'd like to add |
Hi! The It might be best including this in relationship meta? Or just including it as an attribute? Do you have a preference? |
For my application, it would be simplest just to have it as an extra attribute. But it's only needed on a few requests, so it needs to be selectable in the request. |
FYI, I've got this working by extending protected function with(Builder $query, Collection $includePaths): void
{
foreach ($includePaths->toArray() as $include_path) {
if (ends_with($include_path, '_count')) {
// Count
$query->withCount(str_replace_last('_count', '', $include_path));
} else {
// Eager load
$query->with($include_path);
}
}
parent::with($query, $includePaths);
} I can now use (I think eager loading is automatic in 1.x, but I'm still using 0.x). I fully appreciate your comment that I am abusing the The only 'gotcha' I found was that |
Hi, @fisharebest, |
@fisharebest glad you found a solution that works for you at the moment. @dimitvassilev Sparse fieldsets are a good suggestion, though potentially would have to list a lot of fields as you might all of them except for the count. Having looked through the Eloquent documentation on this, I actually think this would be a useful to add to this package, but in a JSON API compliant way. My suggestion would be to add the count to the meta of the relationship, i.e.: {
"data": {
"type": "posts",
"id": "1",
"attributes": {
"title": "Hello World",
"content": "..."
},
"relationships": {
"comments": {
"self": "/api/posts/1/relationships/comments",
"related": "/api/posts/1/comments",
"meta": {
"count": 12
}
}
}
}
} And use a custom parameter to allow it to be included. E.g.:
Or if you wanted multiple relationships counted:
Would be interested to hear your thoughts! |
I guess there could be other meta data to include. With a post, you may want both the number of comments and the date of the most recent. So perhaps a more flexible parameter name along the lines of |
@fisharebest yes, definitely a good suggestion. I've added this to the |
If I'm not mistaken, by the spec, query params must be camel cased, or prefixed by - or _. So includeMeta would work but count= would not. Also, can I suggest using the same approach as with fields? Another thing, I use the meta node in each included relationship to put the pivot table attributes. Would be nice to use something similar for that: What do you think? -- JL |
@JeanLucEsser I think you are mistaken about the spec - it only says that camel-casing is one of the recommended ways to specify multi-word parameters, the other (still only recommended) being by use of the - and _ characters. Neither prefix is required, nor the use of single-word parameters prohibitted. |
Laravel 5.7.10 added a When adding a count feature, would be good to also support this eager loading. ED: one thing to work out is what to do if the resource is side-loaded via an include path... would need to include the count in the side-loaded resource if the client is always expecting it to be there. E.g. if a request asks for |
So how do we do this? Using sparse fields seem to do the count queries regardless if you include them in the fields query params. Also, this was said to be added to the v1.1.0 milestone, but we are way past that. Would be good to document this somewhere if we are able to do it. Any info regarding this is appreciated! |
@webdobe thanks for your comment. this is going to have to wait until the revamp, as I need to sort out the revamp first. Sorry, but maintaining an open source project I have to prioritise, and at the moment the revamp is needed to open the pathway to closing off a lot of these issues. |
Closing in favour of implementing in the new package - |
Laravel provides support to add counts of relations to a model. e.g.
Is there a way to incorporate
$model->withCount(...)
into a laravel-json-api request?The text was updated successfully, but these errors were encountered: