aarondfrancis / fast-paginate

A fast implementation of offset/limit pagination for Laravel.
MIT License
1.21k stars 56 forks source link

withCount() and an orderByDesc() results in a column not found exception (MySQL) #14

Closed realslimsutton closed 2 years ago

realslimsutton commented 2 years ago

I've got the following query:

$products = Product::query()
     ->withCount('orders')
    ->orderByDesc('orders_count')
    ->fastPaginate();

When using fastPaginate(), I get an error with MySQL that the column "orders_count" does not exist. However, when using the standard paginate(), the query works perfectly.

Is there a way I can get this to work using fastPaginate? In my specific situation, I'm attempting to get the most purchased store products hence the count and sort.

Thanks!

seanfoushee commented 2 years ago

I just ran into this today, but the oddity is that it's only affecting certain users and the log showing the SQL error lists the column name as an odd mix of numbers and letters (like a short hash) which obviously doesn't exist in my schema.

Column not found: 1054 Unknown column '53b79b97' in 'where clause'

realslimsutton commented 2 years ago

Sounds like this is a bug then, I'll have a further look now and maybe I can get some more info.

realslimsutton commented 2 years ago

@seanfoushee Can you check if #15 fixes your issue? I've confirmed it fixes my use-case and it might be related to what you're tackling.

seanfoushee commented 2 years ago

@realslimsutton it looks like it should fix my issue, but I need to wait until next week to be sure as my client's office is closed for a conference this week. Thanks!

aarondfrancis commented 2 years ago

@seanfoushee will you also check #16, that's the way I'm currently leaning atm. Thanks yall!

realslimsutton commented 2 years ago

@seanfoushee will you also check #16, that's the way I'm currently leaning atm. Thanks yall!

@aarondfrancis Thank you for the swift fix and responses! I can confirm #16 fixes my situation!