laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.62k stars 11.03k forks source link

PR #53209 changes aggregations with group-by #53581

Closed rikvdh closed 2 days ago

rikvdh commented 2 days ago

Laravel Version

11.33.0

PHP Version

8.3.14

Database Driver & Version

MariaDB

Description

The behavior is changed in PR #53209. I've built queries in my application which I need further on. But now the behavior changed for aggregate-functions like: min(...), max(...).

When having joins and aggregations with custom selects from joined tables I've seen other issues producing invalid SQL queries due to the subquery existing and columns not existing outside the subquery. But I cannot make an isolated testset and the query is too big.

Steps To Reproduce

in 11.32.0:

Model::groupBy('field')->min('column');

returns int.

in 11.33.0:

Model::groupBy('field')->min('column');

returns collection.

rikvdh commented 2 days ago

Ok, I've made a testcase which now is broken in v11.33.0:

Returns::groupBy('orders.locale')
            ->join('orders', 'orders.id', 'order_id')
            ->having('nreturns', '>', 10)
            ->selectRaw('orders.locale,returns.created_at,count(distinct returns.id) as nreturns')
            ->min('returns.created_at')

The having clause seems to break the query, this makes the 'orders.locale' invalid outside the subquery.

crynobone commented 2 days ago

@rikvdh Can you create a simple TestCase that would cover this usage?

taylorotwell commented 2 days ago

Reverted.