如何统计laravel中的数据条数

How to count the number of data in laravel

我需要计算玩家总数中已付款的团队成员人数,例如,id 每个团队有 3 名玩家,只有 2 人付款,它应该显示为 2/3。

我能够使用关系加载玩家 ID(枢轴 table)

这是我的 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,
                           
                        ]
                    );
}

这是 Team Model

中的 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() 时,我遇到了另一个错误

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

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

       @endforeach

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

我得到的错误

我只需要知道怎么算出支付的玩家占队伍总人数的2/3

有人请帮助我。 谢谢

您可以在关系本身上调用 count。所以,替换

<td>
    @foreach ($team->competitionPayments $item)
        {{ $item->paid_by }}
    @endforeach
    /{{$team->players->count()}}
</td>

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

就足够了,假设 $team->players 也是 relationshipcollection

你可以做到这一点

<td>

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

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

或者你可以这样做

<?php $count = 0 ?>

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

    <?php $count++ ?>

   @endforeach

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