cybercog / laravel-love

Add Social Reactions to Laravel Eloquent Models. It lets people express how they feel about the content. Fully customizable Weighted Reaction System & Reaction Type System with Like, Dislike and any other custom emotion types. Do you react?

Home Page:https://komarev.com/sources/laravel-love

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.