The idea is to get the top sale products from the order table.
Order::select('product_id', 'price')->get()->groupBy('product_id')
->map(function ($row) {
return $row->count('product_id');
});
The Order table looks like this
I get something like:
product_id: count
"8" : 2,
"34": 1,
"36": 1,
"28": 1,
"31": 1,
"40": 1,
"44": 1,
"46": 1,
"47": 2
But I still need to order by the count. How do I do it?
Expected
"8" : 2,
"47": 2,
"34": 1,
"36": 1,
"28": 1,
"31": 1,
"40": 1,
"44": 1,
"46": 1,