-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Laravel hasManyThrough returns wrong withCount but good ->count() #30575
Comments
I can find: #26623 which got fixed by #26676 but maybe this doesn't works for polymorphic relationships. I've also found #26635 (comment) which maybe helps? @staudenmeir do you know if you kept polymorphic relationships into account with your PR from above? |
@driesvints I do not make use of public function index()
{
$posts = Post::query()
//->withCount('commentators as total_commentators_count')
->latest()
->with([
'user',
'tags' => function($query) {
/** @var \Illuminate\Database\Eloquent\Builder $query */
$query->orderBy('name', 'ASC');
},
'commentators',
])
->withCount('commentableComments as total_comments_count')
->when(auth()->id(), function ($query) {
/** @var \Illuminate\Database\Eloquent\Builder $query */
return $query->withCount(['commentableComments as user_comments_count' => static function ($innerQuery) {
/** @var \Illuminate\Database\Eloquent\Builder $innerQuery */
$innerQuery->where('user_id', auth()->id());
}]);
})
->filter([
'year' => request('year'),
'month' => request('month'),
])
->paginate(12);
return view('posts.index', compact('posts'));
} Freek dot dev linked an interesting article very recently that I will try soon, considering how HasManyThrough() is "very confusing" indeed : stitcher dot io/blog/laravel-custom-relation-classes |
This is not a Laravel issue. Aggregate queries with Use a Post::withCount([
'commentators as total_commentators_count' => function (Builder $query) {
$query->select(DB::raw('count(distinct users.id)'));
},
])->get();
Relationships should never have |
@musapinar please see the answer above. @staudenmeir thanks for checking in 👍 |
Description:
I eager load a relationship so that it can return the count of distinct commentators of a blog post. Such as this:
Now in my view, the following will not return the correct count:
$post->total_commentators_count
but the following will return the correct count:
$post->commentators->count()
How come?
Here are some screenshots (I blurred the unnecessary code for convenience) :
https://i.imgur.com/bNcrVid.png
https://i.imgur.com/WlzVqAs.png
Here is my commentators() relation (within a Commentable.php trait that my BlogPost model uses) :
The
comments
table is polymorphic.and here is what the
$posts
(see screenshot), which is supposed to eager loadcommentators
and count it, actually runs in SQL :it seems to be missing all the groupBy and orderByRaw parts?
The whole idea behind this is the following: I'm trying to show how many different people commented on a post (eventhough there are 100 comments, that could be just 2 different users replying to each other).
The text was updated successfully, but these errors were encountered: