Skip to content

Conversation

@tpetry
Copy link
Contributor

@tpetry tpetry commented Jan 20, 2026

Currently, you can add subqueries to a query builder with an alias.

// select (select "allowed_to_x" from "configuration") as "sub" from "users"
User::select([
    'sub' => Configuration::select('allowed_to_x'),
]);

But the same is not yet possible for expressions There's just no way to define an alias. This PR fixes this

// current: select available_a - used_a from "users"
// with pr: select (available_a - used_a) as "remaining_a" from "users"
User::select([
    'remaining_a' => new Expression('available_a - used_a'),
]);

So this PR changes:

  • adds selectExpression (like selectSub)
  • adds expression alias support to select
  • adds expression alias support to addSelect
  • adds units to test the behaviour of array keys for select and addSelect which was not covered yet

@tpetry tpetry force-pushed the 12.x-select-expression branch 2 times, most recently from 4bebe0e to d97f59b Compare January 20, 2026 12:13
@tpetry tpetry force-pushed the 12.x-select-expression branch from d97f59b to 38ccfa0 Compare January 20, 2026 12:17
@tpetry
Copy link
Contributor Author

tpetry commented Jan 20, 2026

The failing static analysis check is because of some blade compiler stuff. Not touched that part.

@taylorotwell taylorotwell merged commit 8b4032f into laravel:12.x Jan 20, 2026
69 of 70 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.

2 participants