如何计算Laravel中的上一行平衡

时间:2017-03-11 06:39:16

标签: php laravel laravel-5 laravel-5.3 laravel-5.4

我有一个Amount Transaction表,其中包含以下数据:

enter image description here

首先我显示余额金额。

enter image description here

控制器代码

 $invaccountstatements = DB::table('tranactions')->whereDate('date', '>=', '2016-06-09')
    ->whereDate('date', '<=', '2016-06-20')->get();

刀片代码

     @foreach($invaccountstatements as $invaccstatements)
     <tr>
    <td>{{$invaccstatements->id}}</th>
    <td>{{$invaccstatements->acc_date}}</th>
    <td>{{$invaccstatements->description}}</th>
    <td style="text-align:right;">  {{number_format($invaccstatements->acc_credit, session('digitsformat'))}}   </td>
    <td style="text-align:right;">{{number_format($invaccstatements->acc_debit, session('digitsformat'))}}</td>
    <td style="text-align:right;"><?php $chkbala = $invaccstatements->acc_credit - $invaccstatements->acc_debit; $tbalance += $chkbala;
    echo number_format($tbalance, session('digitsformat'));?></td>
     </tr> 
   @endforeach

接下来,我为此表添加From Date和To Date Search Filter。

我从日期(09-06-16)到目的地(20-06-16)搜索。

我需要提供以下结果的Laravel查询。

enter image description here

1 个答案:

答案 0 :(得分:2)

您已经有查询来获取所有数据,现在只需添加whereDate()

$transactions = Transaction::whereDate('date', '>=', '2016-06-09')
->whereDate('date', '<=', '2016-06-20')
->get();

使用您心爱的查询构建器

$transactions = DB::table('transactions')
->whereDate('date', '>=', '2016-06-09')
->whereDate('date', '<=', '2016-06-20')
->get();

开始日期余额

$beforeFirstDay = DB::table('transactions')
->whereDate('date', '<=', '2016-06-09')
->get();

//do the same loop you and your friend were doing to calculate the balance
//It will give you starting balance

$originalBalance = 0;
foreach( $beforeFirstDay as $day ) {
    $originalBalance = $originalBalance + ($day->credit - $day->debit);
}