Skip to content
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

Scope joinReactionCounterOfType not working when used with select #253

Open
thanhtoan1196 opened this issue May 9, 2023 · 3 comments
Open

Comments

@thanhtoan1196
Copy link

thanhtoan1196 commented May 9, 2023

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).
@antonkomarev
Copy link
Member

antonkomarev commented May 9, 2023

@thanhtoan1196 You should select reaction_like_count column too, otherwise database don't know how to sort the results. Or remove orderBy method call.

@thanhtoan1196
Copy link
Author

thanhtoan1196 commented May 9, 2023

@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');
});

@antonkomarev
Copy link
Member

antonkomarev commented May 9, 2023

@thanhtoan1196 could you fork this repo and add a failing test for this case? I'll try to find a workaround then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants