php - How to define a dynamic attribute for a many to many relationship in Laravel 5? -
i have many-to-many relationship order
s product
s, follows:
class order extends model { ... public function products() { return $this->belongstomany('app\product') ->withpivot('unit_price', 'quantity'); } }
i can access attributes of relationship using $order->pivot->unit_price
, $order->pivot->quantity
.
but there way can create accessors relationship?
instance, $order->pivot->subtotal
return unit_price * quantity
.
yes can, looking pivot
class, easy implement, here's tutorial you.
Comments
Post a Comment