Laravel - 总和仅适用于每页

Laravel - Aggregate Sum only Works Per Page

在我的控制器中我有这个代码:

public function salesReport(Request $request)
    {

        $billings = DB::table('sales')
            ->select(
                'cust_code',
                'trans_id',
                DB::raw('created_at as created_date'), 'amount',)->orderByRaw('created_at DESC');

        if (isset($request->start_date) && isset($request->end_date)) {
            $billings = $billings->whereBetween('created_at', [$start_date . ' 00:00:00', $end_date . ' 23:59:59']);
            $render['start_date'] = $request->start_date;
            $render['end_date'] = $request->end_date;
        } elseif (isset($request->start_date)) {
            $billings = $billings->where('created_at', $request->start_date);
            $render['start_date'] = $request->start_date;
        }
        $billings = $billings->orderBy('created_at', 'DESC');
        $billings = $billings->paginate(15);
        $billings = $billings->appends($render);
        $data['billings'] = $billings;
        return view('report.bbnaijaonetimebillingsReport', $data);
    }

我想根据所选项目进行金额汇总。

我在视图中这样做了:

<td>{{ $billing->cust_code }}</td>
<td>{{ $billing->trans_id }}</td>
<td>{{ $billing->created_date }}</td>
<td>{{ $billing->amount }}</td>

然后我用它来执行总和:

{{ $billings->sum('amount') }}

但我观察到的是,它对每个分页(显示的页面)执行求和功能。但我不想那样。我想要每个选定项目的总和,即使有 5 页,它也应该全部加起来。

你对 $billings 进行了分页,因此你得到了分页对象的总和。 制作另一个对象 $sumBillings 并放入另一个查询

    public function salesReport(Request $request)
    {

        $sumBillings = DB::table('sales')
        ->select(
           'cust_code', 
           'trans_id',
             DB::raw('created_at as created_date'),
           'amount',
      )               
     ->orderByRaw('created_at DESC')
        if(isset($request->start_date) && isset($request->end_date))
        {
            $sumBillings=$sumBillings->whereBetween('created_at',[$start_date.' 00:00:00',$end_date.' 23:59:59']);
            $render['start_date']=$request->start_date;
            $render['end_date']=$request->end_date;
        }elseif(isset($request->start_date))
        {
            $sumBillings=$sumBillings->where('created_at',$request->start_date);
            $render['start_date']=$request->start_date;
        }         
        $sumBillings= $sumBillings->orderBy('created_at','DESC');
        $sumBillings->get();
        $sumBillings= $sumBillings->appends($render);

$billings = DB::table('sales')
    ->select(
       'cust_code', 
       'trans_id',
         DB::raw('created_at as created_date'),
       'amount',
  )               
 ->orderByRaw('created_at DESC')
    if(isset($request->start_date) && isset($request->end_date))
    {
        $billings=$billings->whereBetween('created_at',[$start_date.' 00:00:00',$end_date.' 23:59:59']);
        $render['start_date']=$request->start_date;
        $render['end_date']=$request->end_date;
    }elseif(isset($request->start_date))
    {
        $billings=$billings->where('created_at',$request->start_date);
        $render['start_date']=$request->start_date;
    }         
    $billings= $billings->orderBy('created_at','DESC');
    $billings= $billings->paginate(15);
    $billings= $billings->appends($render);
    $data['billings'] = $billings; 

return view('report.bbnaijaonetimebillingsReport',$data)->with('sumBillings', $sumBillings);