簡體   English   中英

如何在雄辯的Laravel中過濾計算字段(附加)?

[英]How filter on calculated field (append) in Eloquent Laravel?

我已經在樣板房中計算了物業“價格”。

protected $appends = ['price'];

public function getPriceAttribute()
{
    if ($this->action) return $this->action_price_per_meter*$this->area;
    return $this->price_per_meter*$this->area;
}

當我通過此虛擬字段過濾房間時...

$rooms = Room::where('price','<',100000)->get();

我當然出錯了-找不到列“價格”

如何用Laravel方式解決這個問題?

whereRaw有一個whereRaw方法,可讓您手動構建查詢的該段。

在您的情況下,您需要使用SQL中模型的等效方法:

->whereRaw('price_per_meter * area < 100000')

根據您的action列正在執行的action會更加復雜,但這應該作為起點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM