【发布时间】:2017-07-21 02:59:24
【问题描述】:
假设我有这两个模型:
订购模式:
- 身份证
- 状态(不完整,完整)
物品型号:
- 身份证
- order_id
- 类型
- is_worthy。
.
/**
* Returns the item's price according to its worthy
*/
public function getPriceAttribute()
{
return $this->is_worthy ? 100 : 10; // $
}
到目前为止一切顺利。
现在我想总结一下完整订单的价格。所以我正在这样做:
App\Item::whereHas('order', function ($query) {
$query->where('state', 'complete');
})->sum('price')
但问题是,我的items 表中没有price 列。因为price属性是在Model中生成的。
所以我的问题是,如何总结完整订单的价格?
【问题讨论】:
-
Laravel 有 whereType 吗?错误信息是什么?
-
@FatimahSanni Laravel 具有动态 where 方法,例如 whereXyz(xyz 是表的列名)
标签: php mysql laravel eloquent laravel-query-builder