Php 产品折扣拉维,获得实际价格
我有模型产品,在表产品中我有列Php 产品折扣拉维,获得实际价格,php,laravel,Php,Laravel,我有模型产品,在表产品中我有列折扣和列价格。如何在模型中使用折扣更新价格?并得到实际价格和旧价格?我有以下职能: class Product extends Model { protected $guarded = []; public function getPriceAttribute() { return $this->price * (1 - $this->discount / 100); } } 使用属性price我可以获得更新的价格,但是如何获得此产品的
折扣
和列价格
。如何在模型中使用折扣更新价格?并得到实际价格和旧价格?我有以下职能:
class Product extends Model
{
protected $guarded = [];
public function getPriceAttribute() {
return $this->price * (1 - $this->discount / 100);
}
}
使用属性
price
我可以获得更新的价格,但是如何获得此产品的旧价格?您应该使用另一个属性作为折扣价格来获得旧价格
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
您应该使用折扣价格的另一个属性来获取旧价格
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
您正在执行访问器函数(),这就是为什么您在获取价格属性时看到折扣价格。您正在执行访问器函数(),这就是为什么您在获取价格属性时看到折扣价格