Here my db tables:
**orders:**
id, customer_id, date .....etc
**packagings**
id, code, name .....
**products**
id, code, name, weight .......
And 2 manytomany tables with pivot :
**packaging_product:**
packaging_id, product_id, quantity
**packaging_order:**
packaging_id, order_id, quantity
To get total weight of one order i have this attribute in my order model:
public function getWeightAttribute()
{
$tot_weight = 0;
foreach ($this->packagings as $pack) {
$tot_weight += $pack->weight * $pack->original['pivot_quantity'];
}
return $tot_weight;
}
Is there an more elegant method using eloquance collections ?
via Polarix