I have two models connected via a pivot table with an extra column to track the quantity
class Item extends Eloquent {
public function containers()
{
return $this->belongsToMany('Container', 'items_containers', 'item_id', 'container_id')->withPivot(['quantity']);
}
}
class Container extends Eloquent {
public function items() {
return $this->belongsToMany('Item', 'items_containers', 'container_id', 'item_id')->withPivot(['quantity']);
}
}
And a third model for my orders:
class OrderItem extends Eloquent {
public function originalItem()
{
return $this->hasOne('Item', 'id', 'item_id');
}
public function container() {
return $this->hasOne('Container', 'id', 'container_id');
}
}
What I want is to be able to get the specific data from the pivot table when eager loading the OrderItem models, so I only get the quantity where the item_id
and container_id
matches what's on the order item.
OrderItems::whereOrderId($orderId)
->with(['originalItem', 'container', '???'])
->get();
What's the best way to specify the relationship on the OrderItem, or load it in with with
?