I came across a weird problem that i cant find the answer how to resolve.
For example i have commenting system on a social network site, which has unlimited level replies to parent comment on a status or picture or whatever.
So i am counting replies with $q->withCount('replies'); I have a globalScope which check if user is inactive (active 1 or 0). It works perfectly with not showing inactive user comment and his child subcomments, but it does count wrong total number of replies.
Example code:
$discourse = RockChat::with([
"getAuthor",
"isLiked",
"lastReply",
"category" => function ($q) {
$q->addSelect("name", "id");
},
"subcategory" => function ($q) {
$q->addSelect("name", "id");
},
"replies" => function ($q) {
$q->withCount("likes");
$q->with(["isLiked", "replies" => function ($q) {
$q->withCount("likes");
$q->with(["isLiked"]);
}]);
}])
->withCount("replyNumber")
->withCount("likes")
->where("category_id", $request->get("category"))
->where("subcategory_id", $request->get("subcategory"))
->where("slug", $request->get("id"))
->first();
and a globalScope
protected static function boot()
{
parent::boot();
static::addGlobalScope('user_active', function (Builder $builder) {
$builder->whereHas('getAuthor');
});
}
I quite cant figure out, how to check if parent comment user is inactive, so dount count child replies as total of replies to status or picture comments or whatever.
I tried applyin global scope to withCount relation method.
Thanks!