如何在 Laravel 中的索引视图中汇总总计


How to sum up total in an index view in Laravel

我正在自制CRM中开发发票系统。我尝试汇总所有商品的价格以获得总计。我不知道如何在索引视图中获取总计。

这是我在factures/show.blade中的表格.php

<table class="table table-striped table-bordered">
    <thead>
    <tr>
        <th>Description</th>
        <th class="text-right">Prix</th>
    </tr>
    </thead>
    <tbody>
    @foreach($facture->items as $item)
    <tr>
        <td>{{$item->description}}</td>
        <td class="text-right">{{$item->prix}} $</td>
    </tr>
    @endforeach
    <tr>
        <td class="text-right">TPS</td>
        <td class="text-right">{{ $item->calcultps() }} $</td>
    </tr>
    <tr>
        <td class="text-right">TVQ</td>
        <td class="text-right">{{ $item->calcultvq() }} $</td>
    </tr>
    <tr>
        <td class="text-right">Total</td>
        <td class="text-right">{{ $item->calculgrandtotal() }} $</td>
    </tr>
</tbody>
</table>

这是我在factures/index.blade中的表格.php

<table class="table table-striped">
    <thead><tr>
        <th><p># de facture</p></th>
        <th><p>Date</p></th>
        <th><p>Description</p></th>
        <th><p>Client</p></th>
        <th class="text-right"><p>Montant</p></th>
        <th class="text-right"><p>TPS</p></th>
        <th class="text-right"><p>TVQ</p></th>
        <th class="text-right"><p>Grand total</p></th>
    </tr></thead>
    @foreach($factures as $facture)
        <tr>
            <td><a href="{{ URL::to('factures/' . $facture->id) }}">{{$facture->id}}</a></td>
            <td>{{ $facture->created_at->format('d F Y') }}</td>
            <td>{{ $facture->courtedescription}}</td>
            <td>{{ $facture->client->prenom}} {{ $facture->client->nom}}</td>
            <td class="text-right">{{ $facture->montant}} $</td>
            <td class="text-right">{{ $facture->tps}} $</td>
            <td class="text-right">{{ $facture->tvq}} $</td>
            <td class="text-right"> IDONTKNOW $</td>
        </tr>
    @endforeach
</table>

这是我的项目.php模型

use Illuminate'Database'Eloquent'Collection;
class Item extends 'Eloquent {
    protected $fillable = ['nom', 'prix'];
    public function facture()
    {
        return $this->belongsTo('Facture');
    }
    public function calcultps(){
        $total = DB::table('items')->sum('prix');
        $tps = $total *0.05;
        echo $tps;
    }
    public function calcultvq(){
        $total = DB::table('items')->sum('prix');
        $tvq = $total *0.09975;
        echo $tvq;
    }
    public function calculgrandtotal(){
        $totalsanstaxes = DB::table('items')->sum('prix');
        $tps = $totalsanstaxes *0.05;
        $tvq = $totalsanstaxes *0.09975;
        echo $totalsanstaxes+$tps+$tvq;
    }
}

和我的 Facture.php 模型

use Illuminate'Database'Eloquent'Collection;
class Facture extends 'Eloquent {
    protected $fillable = [];
    public function client()
    {
        return $this->belongsTo('Client');
    }
    public function items(){
        return $this->hasMany('Item', 'facture_id');
    }
}

我的 FacturesController索引函数.php

public function index()
{
    $factures = Facture::all();
    return View::make('factures.index')->withFactures($factures);
}

我的FacturesController显示功能.php

public function show($id)
{
    $facture = Facture::find($id);
    return View::make('factures.show')->withFacture($facture);
}

我尝试了这些(在factures/index.blade.php中)但没有成功:

{{ $item->calculgrandtotal() }}
{{ $items->calculgrandtotal() }}
{{ $facture->items->calculgrandtotal() }}

这将返回一个数组

{{$facture->items}}

但这行不通

{{$facture->items->prix}}

我知道这里有一些我不明白的地方。 我想知道如何在索引视图中打印出每张发票的总计?

我使用了@MattBurrow的评论(谢谢),我设法用这个打印出了我想要的东西:

索引刀片.php

{{ Item::calculgrandtotal() }}

项目.php型号

static function calculgrandtotal(){
    $totalsanstaxes = DB::table('items')->sum('prix');
    $tps = $totalsanstaxes *0.05;
    $tvq = $totalsanstaxes *0.09975;
    return $totalsanstaxes+$tps+$tvq;
}