Did you try $query->whereHas( instead of ->IfWhereHas(?
I created a package to fix this problem: https://github.com/staudenmeir/eloquent-param-limit-fix
It's a bit hacky, but it works: Category::withCount(['translation as title' => function($query) {...
The Laravel portal for problem solving, knowledge sharing and community building.