我有 2 張桌子、袋子(購物車)和產品。我將用戶添加的數量保存在錯誤表中,將銷售價格保存在產品表中。我需要獲取每件商品的 bag.quantity * products.sale_price 總和。我正在使用查詢生成器來獲取總和。我怎樣才能得到那個我可以使用 join 來獲取所有屬性的列表$items = DB::table('bag')->where('bag.user_id' , $user_id)->where('bag.order_id', 0)->join('products','products.id','=','bag.product_id')->get();API鏈接:https://apptest.factory2homes.com/api/get-bag-products/3我需要將每個唯一的product_id 的數量和sale_price 相乘,然后求其總和
1 回答

慕俠2389804
TA貢獻1719條經驗 獲得超6個贊
你可以使用selectRaw來做這樣的事情:
$items?=?DB::table('bag')->where('bag.user_id'?,?$user_id) ????????????->where('bag.order_id',?0) ????????????->join('products','products.id','=','bag.product_id') ????????????->selectRaw('*,?(bag.quantity?*?products.sale_price)?as?totalPrice') ????????????->get();
要獲得所有的總和,您可以使用sum:
$sum=$items->sum('totalPrice');
- 1 回答
- 0 關注
- 142 瀏覽
添加回答
舉報
0/150
提交
取消