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

Php 如何在Laravel的索引视图中汇总总计,php,laravel,laravel-4,Php,Laravel,Laravel 4,我在一家自制的客户关系管理公司做发票系统。我试着把我所有物品的价格加起来,算出总数。 我不知道如何在我的索引视图中获取总计 这是我在factures/show.blade.php中的表格 <table class="table table-striped table-bordered"> <thead> <tr> <th>Description</th> <th class="tex

我在一家自制的客户关系管理公司做发票系统。我试着把我所有物品的价格加起来,算出总数。 我不知道如何在我的索引视图中获取总计

这是我在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>
<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>
{{ Item::calculgrandtotal() }}
以及我的Facture.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;
    }

}
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');
    }

}
myFacturesController的myindex函数

public function index()
{
    $factures = Facture::all();
    return View::make('factures.index')->withFactures($factures);
}
public function show($id)
{
    $facture = Facture::find($id);
    return View::make('factures.show')->withFacture($facture);
}
myshow函数,用于myFacturesController.php

public function index()
{
    $factures = Facture::all();
    return View::make('factures.index')->withFactures($factures);
}
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(谢谢)的注释,并通过以下方式打印出了我想要的内容:

索引.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>
<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>
{{ Item::calculgrandtotal() }}
.php模型

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

尝试在模型函数中使用
return
而不是
echo
。因为echo不会返回任何东西。另外,如果您在集合而不是模型上调用函数,请运行集合并在模型上调用它。As hasMany将返回一个
elount\Collection
对象。@MattBurrow我应该如何在模型上调用它?你可以使它们成为
静态的
方法,你也可以这样调用它们<代码>项::calculgrandtotal(),因为返回类型不会检索任何模型集。然后可以将它们传递到视图中,然后显示它们。