dongwuwan5646
2017-12-22 18:19
浏览 32
已采纳

PHP动态列的总和

I would like to have the sum of the 4th column of a table, and I dry miserably.

My controller:

public function ticket()
{
    $cmdbars = DB::table('bars')
             ->orderBy('updated_at', 'asc')
             ->get();

    return view('bar_iframe', compact('cmdbars'));
}

My view :

<table>
@foreach($cmdbars as $cmdbar)

<tr>
    <td>
        {{ $cmdbar->la_qtt }}
    </td>
    <td>
        {{ $cmdbar->la_denom }}
    </td>
    <td class="txtr">
        {{ number_format($cmdbar->le_tarif_bar/100, 2, '.', ' ') }}
    </td>
    <td class="txtr">
        @php
        $sum_produit = $cmdbar->le_tarif_bar * $cmdbar->la_qtt;
        @endphp
        {{ number_format($sum_produit/100, 2, '.', ' ') }}
    </td>
</tr>
@endforeach

<tr>
    <td colspan="4">
        <div class="total_cmd">
            {{-- HERE, I would like the sum of the 4th column --}} €
        </div>
    </td>
</tr>

I'm looking for a day and I'm blocking on this problem, Thanks for your help

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongqijuan3786 2017-12-22 18:22
    已采纳

    Something like this should be working:

    {{ number_format($cmdbars->sum(function($el) {return $el->le_tarif_bar * $el->la_qtt; })/100, 2, '.', ' ') }}
    

    (assuming you are using Laravel 5.3.+ where Query builder is returning collection of elements - method sum is used here)

    Also I don't see any point in using:

    @php
    $sum_produit = $cmdbar->le_tarif_bar * $cmdbar->la_qtt;
    @endphp
    {{ number_format($sum_produit/100, 2, '.', ' ') }}
    

    Use:

    {{ number_format(($cmdbar->le_tarif_bar * $cmdbar->la_qtt)/100, 2, '.', ' ') }}
    

    instead. Using PHP in Blade (or generally in views) is bad practice and should be avoided if possible.

    EDIT

    Personally I would use Eloquent for this and create Bar model and get bars like this:

    $cmdbars = Bar::orderBy('updated_at', 'asc')->get();
    

    In Bar model I would add accessor:

    public function getPriceAttribute()
    {
       return $this->le_tarif_bar * $this->la_qtt;
    }
    

    and then in view instead of:

    @php
    $sum_produit = $cmdbar->le_tarif_bar * $cmdbar->la_qtt;
    @endphp
    {{ number_format($sum_produit/100, 2, '.', ' ') }}
    

    I would use:

    {{ number_format($cmdbar->price/100, 2, '.', ' ') }}
    

    and to get sum I would use:

    {{ number_format($cmdbars->sum('price')/100, 2, '.', ' ') }}
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongzi5062 2017-12-22 18:25

    Add each result to $total variable:

    @php
        $sum_produit = $cmdbar->le_tarif_bar * $cmdbar->la_qtt;
        $total += $sum_produit;
    @endphp
    {{ number_format($sum_produit/100, 2, '.', ' ') }}
    

    Display $total:

    <div class="total_cmd">
        {{ number_format($total/100, 2, '.', ' ') }} €
    </div>
    
    评论
    解决 无用
    打赏 举报
  • douwen5985 2017-12-22 18:32

    Use Collection::sum() with a callback in your controller and pass the result to the view:

    $cmdbars = DB::table('bars')
             ->orderBy('updated_at', 'asc')
             ->get();
    
    $total = $cmdbars->sum(function ($cmdbar) {
        return $cmdbar->le_tarif_bar * $cmdbar->la_qtt;
    });
    
    return view('bar_iframe', compact('cmdbars', 'total'));
    

    Then you can use {{ $total }} where ever you need it in the view.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题