Answer the question
In order to leave comments, you need to log in
Laravel 5 Collection. Why does Sum work on the local machine, but not on the server?
Good evening!
I came across a very strange oddity. Namely, here's what: I
use the sum method of the laravel collection obtained from Eloquent.
$oReferalls->each(function($oRefer){
$oRefer->deposits->each(function($oDeposit){
$oDeposit['onepays'] = $oDeposit->payments->where('purpose',3)->sum('amount');
$oDeposit['perpays'] = $oDeposit->payments->where('purpose',4)->sum('amount');
});
$oRefer->onepays = round($oRefer->deposits->sum('onepays'),2);
$oRefer->perpays = round($oRefer->deposits->sum('perpays'),2);
});
$aSums = ['onepays'=>0, 'perpays'=>0];
$oDeposit->payments->each(function($oPayment) use (&$aSums){
$oPayment->amount = doubleval($oPayment->amount);
if($oPayment->purpose == 3){
$aSums['onepays'] += $oPayment->amount;
}elseif($oPayment->purpose == 4){
$aSums['perpays'] += $oPayment->amount;
}
});
$oDeposit['onepays'] = $aSums['onepays'];
$oDeposit['perpays'] = $aSums['perpays'];
Answer the question
In order to leave comments, you need to log in
Most likely the problem is in the Collection::where() function , it uses strict(===) comparison.
Try using the Collection::whereLoose() function , which uses a non-strict (==) comparison.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question