我已经计算了模型房间的属性“价格”。
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方式解决这个问题?
最佳答案
Eloquent 有一个 whereRaw
方法,它允许您手动构建该查询段。
在您的情况下,您需要等效于 SQL 中模型的方法:
->whereRaw('price_per_meter * area < 100000')
根据您的 action
列正在执行的操作,它会变得更加复杂,但这应该作为起点。
关于php - 如何在 Eloquent Laravel 中过滤计算字段(追加)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40577568/