【发布时间】:2019-05-08 00:03:13
【问题描述】:
您好,我在产品模型变量 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);
我该如何解决这个问题?
【问题讨论】:
-
因为这个自定义属性不是表格的一部分,所以它不能被查询选择。 Eloquent 查询转换为 SQL 查询。您可以对集合执行过滤,但是对于较大的数据集,这可能会很慢。
-
@Thomas 我能为这个事业做些什么?