我正在从表中查询大型数据集,然后迭代循环以创建 json 文件。
$user = App\User::all();
foreach($user as $val){
// logic goes here for creating the json file
}
Run Code Online (Sandbox Code Playgroud)
现在我面临的问题是,当迭代循环时,它正在消耗内存,并且我收到错误“允许的内存大小已耗尽”。而且服务器的 CPU 使用率变得如此之高。我的问题是我应该如何使用 laravel 惰性集合来解决这个问题。我已经浏览了官方文档但找不到方法。
只需将all方法替换为方法即可cursor。
$user = App\User::cursor();
foreach($user as $val){
// logic goes here for creating the json file
}
Run Code Online (Sandbox Code Playgroud)
有关可以链接的方法的更多信息,请参阅官方文档
| 归档时间: |
|
| 查看次数: |
5558 次 |
| 最近记录: |