laravel chunk returns wrong data

Viewed 76

My problem is that chunk is giving wrong data depending of the chunk size that i provide.. can someone give me a logical explanation to this?

When i $chunk_by = 14 the chunk is returning wrong data but when i use $chunk_by = 100; results are correct.

the results of the below code if $chunk_by = 14:

string(30) "index: 0 displays store: 71360"
string(30) "index: 1 displays store: 71361"
string(30) "index: 2 displays store: 71370"
string(30) "index: 3 displays store: 71371"
string(30) "index: 4 displays store: 72483"
string(30) "index: 5 displays store: 72482"
string(30) "index: 6 displays store: 73283"
string(30) "index: 7 displays store: 73284"
string(30) "index: 8 displays store: 73298"
string(30) "index: 9 displays store: 73297"
string(31) "index: 10 displays store: 74149"
string(31) "index: 11 displays store: 74150"
string(31) "index: 12 displays store: 75246"
string(31) "index: 13 displays store: 78589"
string(30) "index: 0 displays store: 78589"
string(30) "index: 1 displays store: 78740"
string(30) "index: 2 displays store: 78739"
string(30) "index: 3 displays store: 78943"
string(30) "index: 4 displays store: 78944"
string(30) "index: 5 displays store: 82702"
string(30) "index: 6 displays store: 82701"
string(30) "index: 7 displays store: 82875"
string(30) "index: 8 displays store: 82876"
string(30) "index: 9 displays store: 83549"
string(31) "index: 10 displays store: 83548"
string(31) "index: 11 displays store: 84017"
string(31) "index: 12 displays store: 84016"
string(31) "index: 13 displays store: 85766"
string(30) "index: 0 displays store: 85766"
string(30) "index: 1 displays store: 86947"
string(30) "index: 2 displays store: 86946"
string(30) "index: 3 displays store: 87308"
string(30) "index: 4 displays store: 87309"

notice that the result of the new chunk of index 0 the data of the same id of the chunk before and notice that it replaces the one that should be on index 0 ... thus leaving you with missing data and duplicated ones instead !!!

However if we replace $chunk_by = 100:

string(32) "index: 91 displays store: 131585"
string(32) "index: 92 displays store: 131584"
string(32) "index: 93 displays store: 131644"
string(32) "index: 94 displays store: 131645"
string(32) "index: 95 displays store: 135388"
string(32) "index: 96 displays store: 135387"
string(32) "index: 97 displays store: 135668"
string(32) "index: 98 displays store: 135667"
string(32) "index: 99 displays store: 136738"
string(31) "index: 0 displays store: 136739"
string(31) "index: 1 displays store: 137207"
string(31) "index: 2 displays store: 137208"
string(31) "index: 3 displays store: 139294"
string(31) "index: 4 displays store: 139295"
string(31) "index: 5 displays store: 139492"

Thus the result of the new chunk of index 0 gets the new store wallet id that is 136739

$query = StoreWallet::selectRaw('store_wallets.*')->selectRaw('stores.ref as SN')->join('stores', 'stores.id', '=', 'store_wallets.store_id')->where('store_id', 106)->whereBetween('store_wallets.created_at', array('2018-01-01' , '2019-01-12'));

$chunk_by = 14; // 14 vs 100
$total = 0;

$query->chunk($chunk_by, function ($stores_wallets) use (&$total) {   
   // the following for each is for testing purposes; 
   // to check the results                                                                                                                           
   foreach($stores_wallets as $key => $store_w){
      // as you know ids are unique, shouldn't be duplicated
      var_dump('index: ' . $key . ' displays store: ' . $store_w->id);
   }
 });
0 Answers
Related