您好,我在Product模型变量appends
中具有:
protected $appends = ['finalPrice'];
public function getFinalPriceAttribute()
{
if ($this->discount_type == 1) {
return intval($this->price -= $this->discount);
} elseif ($this->discount_type == 0) {
return intval($this->price * (1 - $this->discount / 100));
}
return $this->price;
}
当我想在其间使用此属性时,出现错误:undifined column finalPrice
:
$_products = Product::active()
->whereBetween('finalPrice', [$whereSum['min'], $whereSum['max']])
->orderBy($orderColumn, $orderBy);
我该如何解决?
答案 0 :(得分:0)
您的口才是
$_products = Product::active()
->select('id',DB::raw("(CASE WHEN discount_type = 1 THEN price discount WHEN discount_type = 0 THEN price * (1 - / 100) END) as new_price)"))
->orderBy($orderColumn, $orderBy);