Php 如何在laravel中计算付款和发票页面中的余额?
我有这个页面显示发票和付款,我想计算表中每行末尾的余额。 我的记录是按Php 如何在laravel中计算付款和发票页面中的余额?,php,laravel,Php,Laravel,我有这个页面显示发票和付款,我想计算表中每行末尾的余额。 我的记录是按DESC顺序排序的,这就是为什么我无法将其正确排序的原因。 结果如下图所示,按创建时间排序,我希望从余额中扣除类型付款,最后一行余额列显示$-30,第二列显示$70。 发票金额应添加到余额中 学生模式 public function billings() { return $this->hasMany(Billing::class)->orderBy('created_at', '
DESC
顺序排序的,这就是为什么我无法将其正确排序的原因。
结果如下图所示,按创建时间排序,我希望从余额中扣除类型
付款
,最后一行余额列显示$-30,第二列显示$70。
发票金额应添加到余额中
学生模式
public function billings()
{
return $this->hasMany(Billing::class)->orderBy('created_at', 'desc');
}
public function billingRecords()
{
return $this->hasMany(BillingRecord::class, 'billing_id');
}
计费模式
public function billings()
{
return $this->hasMany(Billing::class)->orderBy('created_at', 'desc');
}
public function billingRecords()
{
return $this->hasMany(BillingRecord::class, 'billing_id');
}
DataTable/HTML
<table class=" table table-bordered table-striped table-hover datatable datatable-Room">
<thead>
<tr>
<th>Date</th>
<th>Type</th>
<th>Amount</th>
<th>Balance</th>
<th> </th>
</tr>
</thead>
<tbody>
@php
$balance = 0;
@endphp
@foreach($student->billings as $key => $billing)
@php
$total = 0;
$tooltip = "";
@endphp
<tr data-entry-id="{{ $billing->id }}">
<td>
<p class="m-0">{{ $billing->created_at->format('M d, Y') ?? '' }}</p>
<p class="small m-0">{{ $billing->created_at->format('h:i A') ?? '' }}</p>
</td>
<td>{{ $billing->type ?? '' }}</td>
<td>
@foreach ($billing->billingRecords as $record)
@php
$total += $record->amount;
@endphp
@endforeach
@if($billing->type == "Payment")
$-{{ $total }}
@elseif($billing->type == "Invoice")
${{ $total }}
@endif
</td>
<td>
$ {{ $balance += $total }}
</td>
</tr>
@endforeach
</tbody>
</table>
日期
类型
数量
平衡
@php
美元余额=0;
@endphp
@foreach($student->billings as$key=>$billing)
@php
$total=0;
$tooltip=“”;
@endphp
{{$billing->created_at->format('md,Y')??'}
{{$billing->created_at->format('h:ia')??'}
{{$billing->type???'}
@foreach($billing->billingRecords作为$record)
@php
$total+=$record->amount;
@endphp
@endforeach
@如果($billing->type==“Payment”)
$-{{$total}
@elseif($billing->type==“发票”)
${{$total}
@恩迪夫
${{$balance+=$total}
@endforeach
当前结果
直接调整
$total
怎么样
<tr data-entry-id="{{ $billing->id }}">
<td>
<p class="m-0">{{ $billing->created_at->format('M d, Y') ?? '' }}</p>
<p class="small m-0">{{ $billing->created_at->format('h:i A') ?? '' }}</p>
</td>
<td>{{ $billing->type ?? '' }}</td>
<td>
@foreach ($billing->billingRecords as $record)
@php
$total += $record->amount;
@endphp
@endforeach
@php
if ($billing->type == "Payment") {
$total *= -1;
}
@endphp
${{ $total }}
</td>
<td>
$ {{ $balance += $total }}
</td>
</tr>
在您的计费
型号中添加以下内容:
public function newCollection(array $models = [])
{
return new BillingCollection($models);
}
你试图解决什么问题?这看起来像是不应该在模板中计算的东西不,它只是显示与金额相同的余额。我看不出答案有什么问题?是金额列还是余额列?谢谢你这么多人,你帮了很多忙:)我感谢你对我的问题的友好回答没有问题。我很高兴,我能帮上忙。这确实是一个艰难的时刻^^