【问题标题】:How to count the number of data in laravellaravel中如何统计数据的数量
【发布时间】:2021-03-19 12:03:04
【问题描述】:

我需要计算已支付球员总数的团队成员数量,例如 id 每支球队有 3 名球员,只有 2 名球员支付了它应该显示为 2/3。

我能够使用关系(数据透视表)加载玩家 ID

这是我的刀片的样子

    @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。

请有人帮帮我。 谢谢

【问题讨论】:

    标签: php laravel eloquent count laravel-7


    【解决方案1】:

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

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

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

    就足够了,假设$team-&gt;players 也是relationshipcollection

    【讨论】:

      【解决方案2】:

      你可以这样做

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

      或者你可以这样做

      <?php $count = 0 ?>
      
      <td>
         @foreach ($team->competitionPayments as $item)
      
          <?php $count++ ?>
      
         @endforeach
      
          /{{$team->players->count()}}
      </td>
      

      【讨论】:

        猜你喜欢
        • 2018-04-19
        • 1970-01-01
        • 1970-01-01
        • 2020-11-21
        • 2015-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-07
        相关资源
        最近更新 更多