我想用customer表做一些事情并且有很多记录所以我将使用chunk函数来完成每100条记录。我还想添加一个进度条来显示进度。
但我写的代码不起作用。错误发生在“$ bar”变量。
$count = DB::table('customers')->count();
$bar = $this->output->createProgressBar($count);
DB::table('customers')->chunk(100, function ($customers,$bar) {
foreach($customers as $customer) {
//do something with customer
$bar->advance();
}
$bar->finish();
}
这是正确的方法:
->chunk(100, function ($users) use ($bar){}