为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 79 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信