如何在 Laravel 中计算数据数量

3

我需要计算已付款的团队成员数量与球员总数之比,例如每个团队有3名球员,只有2名球员付款,则应显示为2/3。

enter image description here

我使用关系(中间表)成功加载了球员ID。

以下是我的blade模板代码:

    @if(!empty($teams)&&count($teams)>0)
@foreach($teams as $key=>$team)
<br><br><hr><br><br>
<table>
    <tr>
      <th>id</th>
      <th>Team Name</th>
      <th>Captain Name</th>
      <th>Status</th>
      <th>Num of Players Paid</th>
      <th>Payment</th>
      <th>Time Remaining</th>
    </tr>
    <tr>
      <td>{{$key+1}}</td>
      <td>{{$team->name}}</td>
      <td>{{$team->captain->full_name}}</td>
      <td>{{$team->pivot->status}}</td>
        <td>
           @foreach ($team->competitionPayments $item)

           {{ $item->paid_by }}

           @endforeach

            /{{$team->players->count()}}
        </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>
    </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';
                })->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>


@endforeach
@else
<tr>
    <td colspan="6">
        <div class="text-muted small text-center">
            No teams have registered yet
        </div>
    </td>
</tr>
@endif

这是我的控制器函数

 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,
                           
                        ]
                    );
}

这是“团队模型”中的“competitionPayments”关系。
 public function competitionPayments()
{
    return $this->hasMany(CompetitionPayment::class, 'team_id');
}

我尝试多种方式添加count()方法,但遇到了错误。

<td>
       @foreach ($team->competitionPayments as $item)

       {{ $item->paid_by->count() }}

       @endforeach

        /{{$team->players->count()}}
    </td>

当我添加count()时,遇到了这个错误:

当我尝试像这样添加count()时,出现了另一个错误:

enter image description here

<td>
       @foreach ($team->competitionPayments as $item)

       {{ count($item['paid_by']) }}

       @endforeach

        /{{$team->players->count()}}
    </td>

我遇到的错误: enter image description here 我只需要知道如何计算支付的球员人数与球队总人数为2/3的比例。 请帮帮我,谢谢。
2个回答

2
你可以在关系本身上调用count。因此,替换
<td>
    @foreach ($team->competitionPayments $item)
        {{ $item->paid_by }}
    @endforeach
    /{{$team->players->count()}}
</td>

使用

<td>{{ $team->competitionPayments->count() . '/' . $team->players->count() }}</td>

假设$team->players也是一个relationship或者collection,那么这就足够了。


0

你可以做到这个

<td>

   {{ count($team->competitionPayments) }}

   /{{$team->players->count()}}
</td>

或者你可以这样做

<?php $count = 0 ?>

<td>
   @foreach ($team->competitionPayments as $item)

    <?php $count++ ?>

   @endforeach

    /{{$team->players->count()}}
</td>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接