2016-07-18 14 views
2

私はcharity_idを保持するcharity_donationsテーブルを持っています。これは見た目です。私がする必要がどのようなLaravel 5.2の慈善団体の合計金額を表示します。

Charity Donations Table

は、私は、各慈善IDをGROUPBY、その特定の慈善のために寄付されたどのくらいのお金をカウントする必要があります。そして、最も重要なのは、私はそれをビューに表示する必要があるということです。

このようなもの:

チャリティID |合計

1 | $ 1,200の ....

私はこれを試してみました、チャリティーIDをカウントし

$displayPerCharity = CharityDonation::select(DB::Raw('charity_id, COUNT(*) as    count'))->groupBy('charity_id')->get(); 

     dd($displayPerCharity); 

は、その後、私は、各慈善の合計を提供します。しかし、私はそれぞれの慈善団体の総額が必要であり、それから視野に入れて表示します。

答えて

0

ビューで次に
$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 
0

まあは完全にあなたがそれを見てみたいか次第です。

それぞれの慈善団体の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> 
+0

これはうまくいきます。ありがとう!私はそれがもっと複​​雑になると思った。笑 – David

関連する問題