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

[10.x] Support providing subquery as value to where builder method #48116

Merged

Conversation

gdebrauwer
Copy link
Contributor

Currently, it is impossible to provide a subquery as the $value in a where builder method. You can only achieve a where with a subquery by providing the subquery as the first parameter. When doing that, you must also wrap the column name in DB::raw() otherwise the Builder will think the column name is just a raw value. When you use an operator, you also have to reverse that operator, which makes the code less readable/logical in my opinion.

User::where(
    Game::select('min_score')->whereColumn('games.id', 'users.game_id'),
    '<',
    DB::raw('score')
)

This PR fixes that, so you can just provide the column name first, and then provide the subquery as the “value” parameter. That makes the code cleaner and more readable.

User::where(
    'score',
    '>',
    Game::select('min_score')->whereColumn('games.id', 'users.game_id'),
)

I targeted the 10.x branch because I think changing the parameter signature of a protected method is not a breaking change, but I'm not sure. If that is regarded as a breaking change, I will create a new PR against the master branch.

@taylorotwell taylorotwell merged commit 54421e4 into laravel:10.x Aug 21, 2023
21 checks passed
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

Successfully merging this pull request may close these issues.

None yet

2 participants