ahmeda's avatar

How to sort by multi columns with cursorPaginate

I have this code:

 $products = OrderProduct::query()
            ->with([
                'product' => function ($query) {
                    $query->select('id', 'title', 'price');
                },
                'order' => function ($query) {
                    $query->paid();
                }
            ])
            ->selectRaw('
                product_id,
                SUM(quantity) as quantity_sold,
                SUM(price * quantity) as total_sales
            ')
            ->groupBy('product_id')
            ->orderByDesc('product_id')
            ->cursorPaginate();

How to sort by total_sales desc with cursor paginate, i put total_sales instead of product_id the first page is working when I press next I got this error

Unable to find parameter [total_sales] in pagination item.
0 likes
1 reply
Niush's avatar

@ahmeda Looks like it's a bug in Laravel, related to this. It was fixed for paginate and simplePaginate, but probably not fixed for cursor pagination.

You can use ->simplePaginate() instead, which I think is better suited in your case anyway. Hope it helps.

Please or to participate in this conversation.