【问题标题】:How to add two numbers in the blade Laravel如何在刀片 Laravel 中添加两个数字
【发布时间】:2021-03-20 11:05:30
【问题描述】:

我需要添加我的刀片视图中显示的两个数字/金额,如下图所示

我需要显示为 666.68 / 1000 AUD

这是我的blade 代码

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

          <br>

         {{ $team->competitionPayments }}
         /
         @foreach ($team->competitions as $total)
          {{ $total->pivot->total_fee .' AUD'}}
         @endforeach
   </td>

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

这是Team model 中的competitionPayments 关系

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

请告诉我如何完成这项工作

【问题讨论】:

  • $team-&gt;competitions-&gt;sum('total_fee') ?
  • @Kamlesh Paul 不,我需要得到总金额
  • 然后试试$team-&gt;competitionPayments-&gt;sum('amount')
  • @KamleshPaul 成功!成功了,谢谢!

标签: php laravel count laravel-blade laravel-7


【解决方案1】:

这里的competitionPayments 是一个集合,所以你可以在它上面应用sum() 函数

<td>
  {{ $payment->competitionPayments->sum('amount') }}
<br>

参考链接https://laravel.com/docs/8.x/collections#method-sum

【讨论】:

    【解决方案2】:

    这行得通

    <td>
        {{ $team->competitionPayments->sum('amount') }}
        /
        @foreach ($team->competitions as $total)
        {{ $total->pivot->total_fee .' AUD'}}
        @endforeach
    </td>
    

    【讨论】:

      猜你喜欢
      • 2021-03-19
      • 2018-05-22
      • 1970-01-01
      • 2019-08-02
      • 2014-02-07
      • 2020-08-10
      • 1970-01-01
      • 2017-07-17
      • 2020-06-20
      相关资源
      最近更新 更多