1
比方說,我有這2種型號:Laravel查詢生成器 - 在生成的屬性用途和方法
訂貨型號:
- ID
- 狀態(未完成,完成)
項目型號:
- ID
- 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
屬性是在模型中生成的。
所以我的問題是,我如何總結完整訂單的價格?
不Laravel有一個whereType?什麼是錯誤信息? –
@FatimahSanni Laravel擁有動態的方法,如whereXyz(xyz是表的列名) – Paras