Replies: 5 comments
-
As a solution right now, it's possible to load data as plain query: $model
->load(['transactions' => fn ($q) => Transaction::scopeReferralGroups($q->getQuery(), 3)]) |
Beta Was this translation helpful? Give feedback.
-
Hi @punyflash, You can use $model->setRelation(
'transactions',
Transaction::scopeReferralGroups($model->transactions()->getQuery(), 3)->get()
); |
Beta Was this translation helpful? Give feedback.
-
@staudenmeir, yeah, I agree, wasn't paying attention that result is not set, your example seems more reasonable. Though, the issue is still there: adding the limit statement to the relationship query adds a window function to SQL query where it should not be. I can simplify my code example if $model->load([
'transactions' => fn ($q) => $q->select(
DB::raw('DATE(completed_at) as date'),
DB::raw('COUNT(*) as count'),
DB::raw('SUM(credits_amount) as credits_total'),
)
->where('is_referral', true)
->orderBy('date', 'desc')
->groupBy('date')
->limit(3) // breaks with limit
]); |
Beta Was this translation helpful? Give feedback.
-
why not? $model->load(['transactions' => fn ($q) => $q->referralGroups(3)]); |
Beta Was this translation helpful? Give feedback.
-
The result is not set because your query is not a valid relationship query. It doesn't select actual transactions but custom aggregate rows. The result can only be set if you select the Laravel 11's limit implementation "breaks" your query, but the query itself is already invalid in combination with |
Beta Was this translation helpful? Give feedback.
-
Laravel Version
11.0.7
PHP Version
8.3.3
Database Driver & Version
PgSQL 15.6
Description
I've used this query to display total amount of transactions on a day basis:
This works correctly on Laravel 10.x or without
limit
statement, but now fails with error:Right now, adding a
limit
clause produces a database query wrapped in window function:However expected query is:
I believe this is something to do with #49695. I think this should not be implemented as default behavior, but rather added with condition, for example like
limit(3, window: true)
Steps To Reproduce
Add
limit
clause to any ordered group by query onHasMany
relationship instanceBeta Was this translation helpful? Give feedback.
All reactions