【问题标题】:Product discount laravel, get actual price产品折扣 laravel,获取实际价格
【发布时间】:2019-04-18 13:33:24
【问题描述】:

我有模型产品,在表格产品中我有列discount 和列price。如何使用模型中的折扣更新价格?并获得实际价格和旧价格?我有功能:

class Product extends Model
{

protected $guarded = [];

public function getPriceAttribute() {
    return $this->price * (1 - $this->discount / 100);
} 

}

有了price属性,我得到了更新的价格,但是我怎样才能得到这个产品的旧价格呢?

【问题讨论】:

标签: php laravel


【解决方案1】:

您应该使用另一个属性来获得折扣价以获取旧价格。

class Product extends Model
{

    protected $guarded = [];

    public function getDicountedPriceAttribute()
    {
        return $this->price * (1 - $this->discount / 100);
    }
}

$product->price //old price
$product->discounted_price //price with discount

【讨论】:

    猜你喜欢
    • 2019-02-28
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 2020-01-15
    • 2019-06-06
    • 1970-01-01
    相关资源
    最近更新 更多