Laravel雄辩:计算总价格的最佳方式 [英] Laravel Eloquent: Best Way to Calculate Total Price

查看:139
本文介绍了Laravel雄辩:计算总价格的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个简单的买卖应用程序与Laravel 5.1。每个买入模型都有很多BuyDetail存储买入的物料数量和buy_price。我已经实现了模型表之间的关系。

  class Buy扩展模型
{
#Eloquent关系

public function supplier()
{
return $ this-> belongsTo('App\Supplier');
}

public function buyDetails()
{
return $ this-> hasMany('App \BuyDetail');
}
}

我想计算每个购买。使用Eloquent ORM计算总价格的最佳方式是什么?



现在我只是这样实现:

  @foreach($ buys as $ key => $ value)
<?php
$ total = 0;
?>
@foreach($ value-> buyDetails as $ k => $ bD)
<?php
$ total + =($ bD ['buy_price'] * $ bD [ '数量']);
?>
@endforeach

< tr>
< td> {{$ value-> ref_number}}< / td>
< td> {{$ value-> suplier-> name}}< / td>
< td> {{$ value-> created_at}}< / td>
< td> {{$ value-> buyDetails-> count()}}< / td>
< td> {{$ total}}< / td>
< td>
< a href =class =btn btn-default btn-smtitle =show>细节< / a>
< a href =class =btn btn-primary btn-smtitle =edit>编辑< / a>
< a href =class =btn btn-danger btn-smtitle =delete>删除< / a>
< / td>
< / tr>
@endforeach


解决方案

(至少)2种方式。



使用纯粹的雄辩模型逻辑:

  class Buy extends Model 
{
public function getTotalPrice(){
return $ this-> buyDetails-> sum(function($ buyDetail){
return $ buyDetail - > quantity * $ buyDetail-> price;
});
}
}

这里唯一的问题是需要获取所有从数据库中购买详细信息,但这是您需要获取的视图中显示详细信息。



如果您想避免从数据库中获取关系,您可以构建该查询手动:

 类购买扩展模型
{
public function getTotalPrice(){
return $ this-> buyDetails() - > sum(DB :: raw('quantity * price'));
}
}


Im building a simple buy and sell application with Laravel 5.1. Each Buy Model has many BuyDetail which stores bought item quantity and buy_price. I have implement the relationship between table on the Model.

class Buy extends Model
{
  #Eloquent relationships

  public function supplier()
  {
    return $this->belongsTo('App\Supplier');
  }

  public function buyDetails()
  {
    return $this->hasMany('App\BuyDetail');
  }
}

I'd like to calculate the total price for each Buy. What is the best way to calculate the total price using Eloquent ORM?

for now i just implement it like this:

@foreach($buys as $key => $value)
    <?php
        $total = 0;
    ?>
    @foreach($value->buyDetails as $k => $bD)
        <?php
            $total += ($bD['buy_price']*$bD['qty']);
    ?>
    @endforeach

   <tr>
    <td>{{$value->ref_number}}</td>
    <td>{{$value->suplier->name}}</td>
    <td>{{$value->created_at}}</td>
    <td>{{$value->buyDetails->count()}}</td>
    <td>{{$total}}</td>
    <td>
        <a href="" class="btn btn-default btn-sm" title="show">Detail</a>
        <a href="" class="btn btn-primary btn-sm" title="edit">Edit</a>
        <a href="" class="btn btn-danger btn-sm" title="delete">Delete</a>
    </td>
  </tr>
@endforeach

解决方案

This can be done in (at least) 2 ways.

Using pure Eloquent model logic:

class Buy extends Model
{
  public function getTotalPrice() {
    return $this->buyDetails->sum(function($buyDetail) {
      return $buyDetail->quantity * $buyDetail->price;
    });
  }
}

The only issue here is that it needs to fetch all buy details from the database but this is something you need to fetch anyway to display details in the view.

If you wanted to avoid fetching the relation from the database you could build the query manually:

class Buy extends Model
{
  public function getTotalPrice() {
    return $this->buyDetails()->sum(DB::raw('quantity * price'));
  }
}

这篇关于Laravel雄辩:计算总价格的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆