【问题标题】:How can i add two numbers in foreach blade in laravel and display the total如何在 laravel 的 foreach 刀片中添加两个数字并显示总数
【发布时间】:2021-03-19 14:04:06
【问题描述】:

我需要在刀片视图中添加两个由 foreach 传递的数字

如何将这两个数字相加而不是显示为单独的 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


    【解决方案1】:

    试试array_sum() 方法。替换此块

      @foreach ($team->competitionPayments as $amount)
            {{ $amount->amount }}
        @endforeach
        
    

    有了这个

      @php 
      $temparr = (array) $team->competitionPayments
      @endphp
      {{array_sum(array_column($temparr,'amount'))}}
    

    或者直接试试

      @php 
      $sum = 0
      @endphp
      @foreach ($team->competitionPayments as $amount)
            $sum = $sum + $amount->amount
      @endforeach
    

    【讨论】:

    • 它不起作用我得到了这个错误:ErrorException array_sum() 期望参数 1 是数组,给定对象
    • 它也不起作用错误:array_column() 期望参数 1 是数组,给定对象
    • 对不起,我已经将对象转换为数组,现在它应该可以工作了。
    • 它的输出为 '0 / 1000 AUD',不算数;(
    • 你能检查一下你在 $temparr 中得到了什么吗?
    猜你喜欢
    • 2021-03-20
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 2021-02-14
    • 2020-06-20
    • 2021-01-20
    • 2020-06-18
    • 2019-07-29
    相关资源
    最近更新 更多