table 和 laravel 中 4 个字段的总和

Sum of 4 fields in table with laravel

我需要找到每个客户 ID 的 4 个 table 字段值的总和,但我得到所有客户 ID 的总和,我需要每个客户 ID 的总和

 $vehpaymenthtry = Stock::join('bookings', 'bookings.chasis_no', '=', 'stocks.frame_no')
            ->join('branches', 'branches.branch_id', '=', 'bookings.branch_id')
            ->join('amounts', 'amounts.customer_id', '=', 'bookings.customer_id')
            ->select(DB::raw("SUM(finance_amount+cash+cheque_dd+exchange_amount) as totalpayable") ,'bookings.created_at', 'bookings.customer_id', 'bookings.customer_name', 'stocks.model_variant', 'branches.name as brname', 'stocks.color', 'stocks.frame_no', 'stocks.engine_no')
            ->whereDate('bookings.created_at', '>=', $request->input('fromdate'))
            ->whereDate('bookings.created_at', '<=', $request->input('todate'))
            ->where([['amounts.status', '=', 0],['branches.status', 0]])->get();  

这是结果截图

和数据库截图

您需要按 customer_id 分组 像这样:

   $vehpaymenthtry = Stock::join('bookings', 'bookings.chasis_no', '=', 'stocks.frame_no')
        ->join('branches', 'branches.branch_id', '=', 'bookings.branch_id')
        ->join('amounts', 'amounts.customer_id', '=', 'bookings.customer_id')
        ->select(DB::raw("SUM(finance_amount+cash+cheque_dd+exchange_amount) as totalpayable") ,'bookings.created_at', 'bookings.customer_id', 'bookings.customer_name', 'stocks.model_variant', 'branches.name as brname', 'stocks.color', 'stocks.frame_no', 'stocks.engine_no')
        ->whereDate('bookings.created_at', '>=', $request->input('fromdate'))
        ->whereDate('bookings.created_at', '<=', $request->input('todate'))
        ->where([['amounts.status', '=', 0],['branches.status', 0]])->groupBy('bookings.customer_id')->get();