亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 購物車總和基于 2 個不同表的輸入

Laravel 購物車總和基于 2 個不同表的輸入

PHP
長風秋雁 2023-08-26 10:03:01
我有 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');



查看完整回答
反對 回復 2023-08-26
  • 1 回答
  • 0 關注
  • 142 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號