Laravel-汇总总和仅适用于每页

时间:2019-07-09 10:58:24

标签: laravel laravel-query-builder

在我的控制器中,我有以下代码:

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页,也应该全部求和。

1 个答案:

答案 0 :(得分:0)

您对$ 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);