Php 如何在自定义pivot类laravel5中添加变体
我有订单和产品之间的多对多关系。透视表Php 如何在自定义pivot类laravel5中添加变体,php,laravel,Php,Laravel,我有订单和产品之间的多对多关系。透视表order\u product有其他列:价格,数量。 我有自定义的pivot类: class OrderProduct extends Pivot { protected $attributes = ['price', 'quantity']; public function getPriceAttribute($value) { return number_format($value, 2); }
order\u product
有其他列:价格
,数量
。
我有自定义的pivot类:
class OrderProduct extends Pivot
{
protected $attributes = ['price', 'quantity'];
public function getPriceAttribute($value)
{
return number_format($value, 2);
}
public function setPriceAttribute($value)
{
$price = $value - ($value * 0.1);
$this->attributes['price'] = $price;
}
}
class Product extends Model
{
public function orders()
{
return $this->belongsToMany(Order::class, 'order_product')->using('App\Models\OrderProduct')->withPivot('price','quantity');
}
}
存取器工作,但变异器不工作。如何使mutator
setPriceAttribute
使用sync()
,attach()
$value
在setPriceAttribute
中是正确的,但在DB中不应用修改?在相关模型中实现变异器和访问器
class Product extends Model
{
public function getPriceAttribute($value)
{
return number_format($value, 2);
}
public function setPriceAttribute($value)
{
$price = $value - ($value * 0.1);
$this->attributes['price'] = $price;
}
public function orders()
{
return $this->belongsToMany(Order::class, 'order_product')->using('App\Models\OrderProduct')->withPivot('price','quantity');
}
}
希望这有帮助这里有一个链接可以解释这一点