我正在尝试使用mutator更改价格属性,然后再保存到数据库中。我想注入折扣参数,并在如下所示的mutator中使用它。我收到错误消息“找不到列:1054'字段列表中的未知列'折扣'”是否可以在模型构造函数中注入参数?
class Pizza2 extends Model
{
public $table = 'pizza2';
public $timestamps = false;
public $fillable = ['name', 'price'];
public function __construct($d = null)
{
$this->discount = $d;
}
public function setPriceAttribute($value)
{
if ($this->discount) {
$this->attributes['price'] = $value * $this->discount;
} else {
$this->attributes['price'] = $value;
}
}
}