I've been searching for 2 hours on how to build a query with eloquent using groupBy and orderBY.
This is my situation: I have a table orders with these columns:
idOfOrder, quantityOfProduct, userWhoBuy, productBuy, statusOfOrder
I would like to get the 3 most sold products. For this, I need to build a query who group all data from ProductBy, sum theme from quantity and sort theme by desc. But it's terribly difficult.
This my actual query:
$buyObj->select('product',DB::raw('count(*) as total'))
->where('status','=','1')
->groupBy('product')
->orderBy('quantity','desc')
->take(3)
->get();
I get this error :
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'web_projet_exia.buys.quantity' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select
product, count(*) as total frombuyswherestatus= 1 group byproductorder byquantitydesc limit 3) "
Do you know how to fix that ?
Thanks in advance !
GROUP BYthis has no effect. - Jonas Staudenmeirtotal. - Jonas Staudenmeir