This is my very first question. And hope someone can help me.
I am trying to get all orders with there corresponding total amount of order items. And I manage to get the amount per order items.
How to return also in my select query the sum of amount per Order ?
$orders = Order::join('user_category', 'user_category.user_id', '=', 'orders.purchaser_id')
->join('users', 'users.id', '=', 'user_category.user_id')
->with([
'orderItems' => function($query) {
$query->join('orders', 'orders.id', '=', 'order_items.order_id')->join('products', 'products.id', '=', 'order_items.product_id')
->select('order_items.order_id','order_items.quantity', 'products.price', DB::raw('order_items.quantity * products.price AS amount'));
}
])
->select('orders.id', 'users.first_name', 'users.last_name')
->where('user_category.category_id', '=', 1)
->orderBy('orders.id', 'DESC')
->limit(5)
->get();
->withSum('amount')->withSum('amount')after the->select()inside subquery, but still not working. I thinkwithSum()will only work if its an actual column. Base on my query,amountis an alias