related models count
See original GitHub issueWhat is is the desired way to call ‘count’ on a collection?
Say i have a model
User = db.Model.extend({
tableName: 'users',
photos: function(){ return this.hasMany(Photo); }
});
new User({id: 1}).fetch().then(function(user) {
user.photos().count().then(...) // <- seems to be a good way to query 'select count(*) from photos where user_id = ?'
});
Issue Analytics
- State:
- Created 10 years ago
- Comments:36 (22 by maintainers)
Top Results From Across the Web
Django: Count related model where an annotation on the ...
Django: Count related model where an annotation on the related has a specific value and store count in an annotation (or simply: count...
Read more >Loading Eloquent relationship counts by Tim MacDonald
There are several ways to load relationship counts on eloquent models. I'm going to explore the options and introduce you to a new...
Read more >Use aggregate methods to count related models
When you're counting related models in @laravelphp, opt for aggregates! Using the count() method on a collection of related models is a tad...
Read more >Eloquent: Relationships - The PHP Framework For Web Artisans
Counting Related Models ; Other Aggregate Functions; Counting Related Models On Morph To Relationships. Eager Loading. Constraining Eager Loads; Lazy Eager ...
Read more >QuerySet API reference | Django documentation
that has been computed over the objects that are related to the objects in the ... The Blog model doesn't define an entry__count...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Is there a recommended standard way of achieving this yet? I’ve tried this on my
Dataset
model:Basically I want a
comments
array attribute to be populated when retrieving a single model, but acommentCount
for each model when retrieving a collection. This seems to generate the right sort of query:But the response is returning
"commentCount": []
(an empty array, not a number as I’d expect). Any ideas where I’m going wrong?Cheers