Skip to content

[12.x] allow string-based expressions for selectExpression()#58753

Merged
taylorotwell merged 1 commit intolaravel:12.xfrom
tpetry:12.x-select-expression-string
Feb 11, 2026
Merged

[12.x] allow string-based expressions for selectExpression()#58753
taylorotwell merged 1 commit intolaravel:12.xfrom
tpetry:12.x-select-expression-string

Conversation

@tpetry
Copy link
Contributor

@tpetry tpetry commented Feb 11, 2026

So in 12.48.0 I contributed a way to make using SQL functions easier (after some stuff had to be reverted):

// before 12.48.0
$query->select(new Expression("jsonb_agg(DISTINCT col) AS values"))

// with 12.48.0
$query->selectExpressions(new Expression("jsonb_agg(DISTINCT col)", 'values')

but this is still a lot of boilerplate as you must create a new expression object. And that object is really not needed as the method already defines that you pass expressions. So with this PR, any query builder code can be simplified to this:

$query->selectExpression("jsonb_agg(DISTINCT col)", 'values')

That's much simpler code. And in a real example, it looks also more cleanly:

Visits::query()
  ->select('domain')
  ->selectExpression('count(distinct user)', 'users_unique')
  ->selectExpression('count(user)', 'users_total')
  ->groupBy('domain');

No more expressions needed :)


This PR is fully backwards compatible.

@tpetry tpetry changed the title 12.x: allow string-based expressions for selectExpression() [12.x]: allow string-based expressions for selectExpression() Feb 11, 2026
@tpetry tpetry changed the title [12.x]: allow string-based expressions for selectExpression() [12.x] allow string-based expressions for selectExpression() Feb 11, 2026
@taylorotwell taylorotwell merged commit 0bfaece into laravel:12.x Feb 11, 2026
72 checks passed
@tpetry tpetry deleted the 12.x-select-expression-string branch February 11, 2026 19:25
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

Comments