Laravel 产品折扣,获取实际价格

Dum*_*tru 0 php laravel

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

class Product extends Model
{

protected $guarded = [];

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

}
Run Code Online (Sandbox Code Playgroud)

通过属性,price我可以获得更新的价格,但是如何获得该产品的旧价格?

Ind*_*ing 5

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

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
Run Code Online (Sandbox Code Playgroud)