I want to convert the following sql statement into eloquent:
select order_number, order_id, sum(quantity) as qty, sum(price) as pr from `orders` right join `order_details` on `orders`.`id` = `order_details`.`order_id` group by `order_number`
I did this as like below:
$order = OrderDetails::select(DB::raw('order_number, order_id, sum(quantity) as qty, sum(price) as pr')) ->from('orders') ->rightJoin('order_details', 'orders.id', '=', 'order_details.order_id') ->groupBy('order_number') ->get(); dd($order);
The problem is that it is converting to the same sql but showing error. The error is shown below: *
Illuminate Database QueryException (42000) SQLSTATE[42000]: Syntax error or access violation: 1055 'techvillege.order_details.order_id' isn't in GROUP BY (SQL: selectorder_number, order_id, sum(quantity) as qty, sum(price) as pr from
orders
right joinorder_details
onorders
.id
=order_details
.order_id
group byorder_number
)
* The tables are: orders table order_details I really do not know what is the exact problem here and stuck into it for the last two days. Please someone help me out. Thanks in advance.
Advertisement
Answer
it says that u need to add order_id
to groupBy()
method
$order = OrderDetails::select(DB::raw('order_number, order_id, sum(quantity) as qty, sum(price) as pr')) ->from('orders') ->rightJoin('order_details', 'orders.id', '=', 'order_details.order_id') ->groupBy('order_number', 'order_id') ->get();