I have 4 tables. (I have more columns for each table, but listing only the required once here)
order_header:
id
ship_method
order_status
ship_method:
id
status:
code
order_items:
header_id
This is my order_header model's relationship with order_items:
public function order_items()
{
return $this->hasMany('\App\Models\OrderProduct', 'header_id', 'id');
}
This is my final query to get order_header data:
$orders = OrderHeader::select('order_header.*', 'shipping_method.name as shipping_method_name', 'status.description as status_name')
->join('shipping_method', 'shipping_method.id', '=', 'order_header.ship_method')
->join('status', 'status.code', '=', 'order_header.order_status')
->with(['order_items', 'creator:id,username', 'updater:id,username']);
This returns the $orders as below with it's order_items:
ex: id 17
"data": {
"id": 17,
"shipping_method_name": "shipping method 1 update",
"status_name": "pending",
"order_items": [
{
"id": 35,
"header_id": 17,
"order_quantity": 5,
"total_line_value": "20.00",
},
{
"id": 36,
"header_id": 17,
"order_quantity": 5,
"total_line_value": "20.00",
},
{
"id": 37,
"header_id": 17,
"order_quantity": 5,
"total_line_value": "20.00",
}
}
Now how can I sum the values of order_items' order_quantity and total_line_value and display? For example: at above id 17, I need to display two more lines as order_quantity_sum: 15 and total_line_value_sum: 60. How to achieve this please someone help me. Thanks in advance.