Here is the relation
- transaction
hasMany
Carts - Cart
belongsTo
product - product
->
price
here is the Model class
//# TransactionModel
public function getCarts(){
return $this->hasMany(CartModel::class, 'transaction_id','id');
}
//# CartModel
public function getProduct(){
return $this->belongsTo(ProductModel::class,'product_id','id');
}
what i want to achieve is to get total price of current transactions(many)
what i do now is still iterate per transation and sum the price in $total
Class TransactionModel{
public static function getTotalPrice($transactions){
$total = 0;
foreach($transactions as $transaction){
$total += $transaction->getCarts->sum('getProduct.price');
}
return $total;
}
how to this in eloquent code thanks
$transaction->getCarts->product->sum('price')
– Iftikhar uddincurrent transation
. what we want to achieve is total price ofmany transaction(S)
– david valentino