【发布时间】:2015-09-12 14:27:42
【问题描述】:
我正在使用 Laravel 5 LAMP 堆栈,并且正在尝试使用数据库事务处理 CSV 导入。代码如下所示:
// Should contain any messages to pass back to the user
$results = [];
// Contains the total records inserted
$total = 0;
DB::transaction(function() use($csv_file, $results, $total) {
// Some Code ...
$total++;
$results[] = 'Row 10 has some weird data...';
});
return view('plan.import')
->with('results', $results)
->with('total', $total);
最后,我的记录被导入,但我的 $total 和 $results 仍然是空的,因为它们超出了关闭的范围。我知道它们在函数内部被改变了,因为我已经通过它,看到它们发生了变化。我只是不知道如何让他们退出该交易并将它们归还给用户。有人可以帮忙吗?
【问题讨论】:
标签: php laravel transactions closures