S
S
SWFaust2015-12-14 23:36:27
Laravel
SWFaust, 2015-12-14 23:36:27

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);
        });

Actually on the local machine (OpenServer) everything works as it should. Everything adds up correctly. On the server, nothing is summed up.
I brought the collection through dd on the localhost and on the server - I found this moment:
On the server, all numbers are perceived as strings (actually, as far as I understand this is logical, it seems like all numeric data from the database are returned as strings).
Actually, the question is why Laravel converts strings to numbers in some conditions, but not in others. Maybe someone has come across something similar... Or maybe it's not about laravel at all, but it's worth digging into the php or mysql settings (in principle, I dug right away, compared the configs, didn't find a fundamental difference)?
upd
Actually, as it turned out - it's not about string values. I ran the summable collection in a loop for the test and converted the field summed to a number in all records. Did not help. The result is the same, it summed up normally on the local, but not on the server.
So far I've decided this:
$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'];

But somehow it's not very beautiful when there is an aggregate method for this ... I would like to understand why it works every other time ..

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
D', 2015-12-15
@SWFaust

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.

C
Croshim, 2015-12-14
@Croshim

Use mysqlnd db driver. If you are on ubuntu then:
sudo apt-get install php5-mysqlnd
restart nginx/apache

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question