如何在刀片 Laravel 中添加两个数字
Posted
技术标签:
【中文标题】如何在刀片 Laravel 中添加两个数字【英文标题】:How to add two numbers in the blade Laravel 【发布时间】:2021-03-20 11:05:30 【问题描述】:我需要添加我的刀片视图中显示的两个数字/金额,如下图所示
我需要显示为 666.68 / 1000 AUD
这是我的blade
代码
<td>
@foreach ($team->competitionPayments as $payment)
$payment->amount
@endforeach
<br>
$team->competitionPayments
/
@foreach ($team->competitions as $total)
$total->pivot->total_fee .' AUD'
@endforeach
</td>
这是我的controller
函数
public function detailedRegistrations($competition)
$competitions = $this->competitionsRepo->findOrFail($competition);
$teams = $competitions->teams()->get();
$payments = $competitions->payments()->get();
return view('competitions.detailed-registrations',
[
'teams'=>$teams,
'payments'=>$payments,
'competitions'=>$competitions,
]
);
这是Team model
中的competitionPayments
关系
public function competitionPayments()
return $this->hasMany(CompetitionPayment::class, 'team_id');
请告诉我如何完成这项工作
【问题讨论】:
$team->competitions->sum('total_fee')
?
@Kamlesh Paul 不,我需要得到总金额
然后试试$team->competitionPayments->sum('amount')
@KamleshPaul 成功!成功了,谢谢!
【参考方案1】:
这里的competitionPayments
是一个集合,所以你可以在它上面应用sum()
函数
<td>
$payment->competitionPayments->sum('amount')
<br>
参考链接https://laravel.com/docs/8.x/collections#method-sum
【讨论】:
【参考方案2】:这行得通
<td>
$team->competitionPayments->sum('amount')
/
@foreach ($team->competitions as $total)
$total->pivot->total_fee .' AUD'
@endforeach
</td>
【讨论】:
以上是关于如何在刀片 Laravel 中添加两个数字的主要内容,如果未能解决你的问题,请参考以下文章
如何在 laravel 刀片模板系统的下拉列表中添加属性 ID?