Scope joinReactionCounterOfType not working when used with select
thanhtoan1196 opened this issue · comments
Scope joinReactionCounterOfType not working when used with select
Logs:
Comment::select('comments.name')->joinReactionCounterOfType('Like')->orderBy('reaction_like_count', 'desc')->get();
Illuminate\Database\QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column 'reaction_like_count' in 'order clause' (Connection: mysql, SQL: select `comments.name` from `comments` left join `love_reactant_reaction_counters` as `reaction_like` on `reaction_like`.`reactant_id` = `comments`.`love_reactant_id` and `reaction_like`.`reaction_type_id` = 2 order by `reaction_like_count` desc).
@thanhtoan1196 You should select reaction_like_count
column too, otherwise database don't know how to sort the results. Or remove orderBy
method call.
@antonkomarev Ah I see the problem, if I use addGlobalScope
to select, the query doesn't include COALESCE(reaction_like.count, 0) as reaction_like_count, COALESCE(reaction_like.weight, 0) as reaction_like_weight
, so even if I select reaction_like_count
column too, it still fails.
static::addGlobalScope('scope1', function (Builder $builder) {
$builder->select('comments.name');
});
@thanhtoan1196 could you fork this repo and add a failing test for this case? I'll try to find a workaround then.