I have a problem with getting results of Laravel Excel chunk method outside callback.
I tried to solve this problem by passing variable to callback by use
keyword.
Guided by documentation I tried:
$foo = [[1, 2], [3, 4]];
Excel::filter('chunk')->load('file.csv')->chunk(250, function($results) use(&$foo)
{
foreach($results as $row)
{
$foo[] = [$row[0], $row[1]];
var_dump($foo); // [[1, 2], [3, 4], [5, 6]]
}
});
var_dump($foo); // [[1, 2], [3, 4]]
$foo is passed by reference so I should be able to modify it inside my callback. Problem is, that in last var_dump() my $foo has same values as in the beginning. It is changing only inside the callback.
The only solution I found to be working is passing false as third argument to chunk() function, but that means I will not be able to use queue. It seems that my callback is somehow serialized and I am not able to pass my $foo by reference.
So my question is: how to get $results values to be outside callback?