我有一张charity_donations桌子,里面有一张charity_id,为慈善事业捐赠的款项,以及其他领域的捐款。这就是它的样子。

我需要做的是,我需要groupBy每个慈善机构的ID,然后计算捐了多少钱给那个特定的慈善机构。最重要的是,我需要在视野中展示这一点。
有些事情是这样的:
慈善机构ID
1美元1,200美元。
我试过这个,
$displayPerCharity = CharityDonation::select(DB::Raw('charity_id, COUNT(*) as count'))->groupBy('charity_id')->get();
dd($displayPerCharity);算一下慈善机构的身份证,然后给我每个慈善机构的总数。但我需要每个慈善机构的总金额,然后展示在视野中。
发布于 2016-07-18 01:21:36
明白了!
$displayPerCharity = DB::table('charity_donations')
->select(DB::raw('SUM(amount) as charity_amount, charity_id'))
->groupBy('charity_id')
->orderBy('charity_amount', 'desc')
->get();然后考虑到:
@foreach ($displayPerCharity as $group)
<h1> Charity ID {{ $group->charity_id }} received {{ $group->charity_amount }}</h1>
@endforeach发布于 2016-07-18 01:23:22
你怎么显示它完全取决于你想要它的外观。
如果您只是停留在如何获取每个慈善ID和金额,那么您可以使用一个foreach循环。
下面是一个使用引导响应表的示例。这假设在运行查询之后,将其作为一个名为$displayPerCharity的变量传递给视图。
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Charity ID</th>
<th>Amount ($)</th>
</tr>
</thead>
<tbody>
@foreach($displayPerCharity as $display)
<tr>
<td>{{ $display->charity_id }}</td>
<td>{{ $display->charity_amount }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>https://stackoverflow.com/questions/38427293
复制相似问题