我不知道为什么最后$ count变量的值总是为0,即使我通过引用传递变量并且代码进入循环并且变量增加。
try {
$count = 0;
if ($request->hasFile('file')) {
if ($request->file('file')->isValid()) {
$file = $request->file('file');
Excel::filter('chunk')->load($file->getRealPath())->chunk(100, function($results) use (&$count) {
foreach ($results as $row) {
$count++;
}
});
} else {
throw new \Exception('File not valid');
}
}
$request->session()->flash('alert-info', "{$count}");
} catch (\Exception $e) { }