I have many to many
relation between Order and Product. Pivot table order_product
has additional columns: price
, quantity
.
I have custom pivot class:
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');
}
}
Accessor works but mutator doesn't.
How to make mutator setPriceAttribute
working with sync()
, attach()
?
$value
is correct in setPriceAttribute
, but modifications are not applied in DB?