Php 如何使用laravel查询生成器检索12个月的月度摘要?

Php 如何使用laravel查询生成器检索12个月的月度摘要?,php,mysql,laravel,Php,Mysql,Laravel,我正在做一份报告,其中将显示每个客户从1月到12月的月收入,并且我一直在写正确的mysql查询 这是使用连接两个表的Laravel查询生成器 $so_h = DB::table('a') ->join('b', 'a.book', '=', 'b.book') ->select('a.*', 'b.*') ->whereMonth('a.doc_date', '=','1') -&g

我正在做一份报告,其中将显示每个客户从1月到12月的月收入,并且我一直在写正确的mysql查询

这是使用连接两个表的Laravel查询生成器

$so_h = DB::table('a')
            ->join('b', 'a.book', '=', 'b.book')
            ->select('a.*', 'b.*')
            ->whereMonth('a.doc_date', '=','1')
            ->whereYear('a.doc_date', $current_year)
            ->orderBy('a.book', 'asc')
            ->get();
结果如下所示:

<table>
<tr>
<th>Customer</th>
<th> Jan </th>
<th> Feb </th> 
</tr>
<tr>
@foreach($so_h as $key =>$data)
<td>Customer A</td>
<td> 1.1k </td>
<td> 3k </td>
</tr>
@endforeach
</table>

顾客
简
二月
@foreach($so_h as$key=>$data)
顾客A
1.1k
3k
@endforeach

直到12月

您从查询中得到了什么结果?groupBy应该解决这个问题。我的表b中有total列,当我检索数据时,我可以得到1月份的总数。但是我想在1次查询中得到每个月的总数。您需要在SQL查询中,如
选择日期格式(“2019-07-25”,%Y-%m”)
,并按该字段分组,您从查询中得到什么结果?groupBy应该解决这个问题我的表b中有total列,当我检索数据时,我可以得到1月份的总数。但是我想在1个查询中得到每个月的总数。您需要在SQL查询中,如
选择日期\u格式(“2019-07-25”,%Y-%m”)
,并按该字段分组,