Laravel Eloquent: sum with groupBy

前端 未结 3 1013
名媛妹妹
名媛妹妹 2020-11-27 20:46

Need eloquent/fluent query to get sum with groupBy function.

So far I have tried:

    $this->data[\'no_of_pages\'] = Document::sum(\'no_of_pages\'         


        
3条回答
  •  失恋的感觉
    2020-11-27 21:29

    Little bit refactored and convenient answer is (from @keroles Monsef)

    Document::Where('some_condition',true)
        ->selectRaw("SUM(debit) as total_debit")
        ->selectRaw("SUM(credit) as total_credit")
        ->groupBy('id')
        ->get();
    

提交回复
热议问题