【发布时间】:2021-03-19 14:04:06
【问题描述】:
如何将这两个数字相加而不是显示为单独的 333.34 333.34, 我需要显示为 666.68 / 1000 AUD
这是刀片 foreach 的样子
@if(!empty($teams)&&count($teams)>0)
@foreach($teams as $key=>$team)
<table>
<tr>
<th>id</th>
<th>Team Name</th>
<th>Payment</th>
<th>Time Remaining</th>
<th>Num of Players Paid</th>
<th>Paid out of</th>
<th>Captain Name</th>
<th>Status</th>
</tr>
<tr>
<td>{{$key+1}}</td>
<td>{{$team->name}}</td>
<td>{{($team->pivot->status==0)?'PENDING':(($team->status==1)?'REGISTERED':(($team->pivot->status==3)?'REMOVED':(($team->pivot->status==2)?'WITHDRAWN':'')))}}</td>
<td>
@if ($team->pivot->status == 0)
{{\Carbon\Carbon::createFromTimeStamp(strtotime($team->pivot->created_at.' +1 day'))->diffForHumans(null, true, true, 2)}}
@else
Registered at {{$team->pivot->updated_at->todatestring()}}
@endif
</td>
<td>{{ $team->competitionPayments->count() . '/' . $team->players->count() .' PAID'}}</td>
<td>
@foreach ($team->competitionPayments as $amount)
{{ $amount->amount }}
@endforeach
/
@foreach ($team->competitions as $total)
{{ $total->pivot->total_fee .' AUD'}}
@endforeach
</td>
<td>{{$team->captain->full_name}}</td>
<td>{{$team->pivot->status}}</td>
{{-- <td>{{($team->pivot->status==0)?'PENDING':(($team->status==1)?'REGISTERED':(($team->pivot->status==3)?'REMOVED':(($team->pivot->status==2)?'WITHDRAWN':'')))}}</td> --}}
</tr>
</table>
<table>
<tr>
<th>Full Name</th>
<th>DOB</th>
<th>Active Kids Voucher AKV</th>
<th>Accept Reject AKV</th>
<th>Paid $</th>
<th>Paid By </th>
<th>Total</th>
<th>Stripe</th>
<th>Mobile</th>
<th>Email</th>
<th>T&C</th>
</tr>
@foreach ($team->players as $player)
<tr>
<td>{{ $player->full_name }}</td>
<td>{{ $player->dob }}</td>
<td>-</td>
<td>-</td>
<td>
{!! $player->competitionPayments->isNotEmpty() ?
implode($player->competitionPayments->map(function($item, $key) {
return ($key > 0 ? '<div class="mb-1"></div>' : '') . number_format($item->amount, 2) . ' AUD <br> <hr>' . $item->updated_at->todatestring();
})->toArray()) : '-' !!}
</td>
<td>{{ $player->competitionPayments->isNotEmpty() ? $player->competitionPayments->implode('paidBy.name') : '-' }}</td>
<td>-</td>
<td>-</td>
<td>{{ $player->mobile }}</td>
<td>{{ $player->email }}</td>
<td>-</td>
</tr>
@endforeach
</table>
<br><br><hr><br><br>
@endforeach
@else
<tr>
<td colspan="6">
<div class="text-muted small text-center">
No teams have registered yet
</div>
</td>
</tr>
@endif
这是我Team model中的关系
public function competitionPayments()
{
return $this->hasMany(CompetitionPayment::class, 'team_id');
}
这是我的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,
]
);
}
我尝试添加sum() 并得到如下总数,但我得到了错误的总数并重复了
<td>
@foreach ($team->competitionPayments as $amount)
{{ $amount->sum('amount') }}
@endforeach
/
@foreach ($team->competitions as $total)
{{ $total->pivot->total_fee .' AUD'}}
@endforeach
</td>
这是我得到的总数
请有人帮助我或建议我可以解决此问题的方法
谢谢
【问题讨论】:
标签: php laravel sum laravel-6 laravel-7