【问题标题】:Wherebetween closure laravel在关闭 laravel 之间
【发布时间】: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 我能为这个事业做些什么?

标签: php laravel


【解决方案1】:

你的口才是:

$_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);

【讨论】:

    猜你喜欢
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 2019-06-10
    • 1970-01-01
    相关资源
    最近更新 更多