How to count the number of data in laravel

Viewed 741

I need to count the number of team members who have paid out of the total number of player for example id there's 3 players per team and only 2 have paid it should display as 2/3.

enter image description here

I was able to load the players ID using the relationship (pivot table)

Here' how my blade looks like

    @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

Here's my controller function

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

Here's the competitionPayments relationship in the Team Model

 public function competitionPayments()
{
    return $this->hasMany(CompetitionPayment::class, 'team_id');
}

I tried to add the count() method many ways and i ran into an error

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

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

       @endforeach

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

I got this error when i added count

enter image description here

When i tried to add count() this way i got another error

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

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

       @endforeach

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

Error i got enter image description here

I just need to know how can i count the number of players who have paid out of the total number of players in the team as 2/3.

Someone please help me out. Thanks

2 Answers

You can call the count on relationship itself. so, replacing

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

with

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

will be enough, that is assuming $team->players is also a relationship or a collection.

you can do this

<td>

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

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

or you can do this

<?php $count = 0 ?>

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

    <?php $count++ ?>

   @endforeach

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