之间的封闭laravel

时间:2018-12-06 10:07:48

标签: php laravel

您好,我在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);

我该如何解决?

1 个答案:

答案 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);