Laravel 4 如何在Laravel中获得订单明细之和
我必须使用表来保存订单和订单详细信息 订单Laravel 4 如何在Laravel中获得订单明细之和,laravel-4,Laravel 4,我必须使用表来保存订单和订单详细信息 订单 id name id name quantity price order_id 订单详细信息 id name id name quantity price order_id 这就是我得到总价的方式 Transaction.php public function details() { return $this->hasMany('Detail'); } public function getTotalPriceAttribute
id
name
id
name
quantity
price
order_id
订单详细信息
id
name
id
name
quantity
price
order_id
这就是我得到总价的方式
Transaction.php
public function details()
{
return $this->hasMany('Detail');
}
public function getTotalPriceAttribute()
{
return $this->details()->sum(DB::raw('quantity * price));
}
有没有更优雅的方法?你可以在
$this->details()->get()
上执行reduce
操作,但我认为这会导致更多的代码行,而没有更多的可读性。但是reduce
是一种乐趣。