猿问

使用 eloquent 一一检索大量行

我需要从 MySQL 数据库中检索大量行,但我无法将其全部加载到变量中,因为它太大了。有没有办法像使用 eloquent 查询一样加载行 pdo?


$data = $pdo->query("SELECT * FROM users")->fetchAll();

foreach ($data as $row) {

    echo $row['name']."<br />\n";

}

我想使用 eloquent 查询构建器做同样的事情


万千封印
浏览 95回答 1
1回答

慕运维8079593

使用分块结果chunk()是您的最佳选择。DB::table('users')->chunk(100, function ($users) {&nbsp; &nbsp; foreach ($users as $user) {&nbsp; &nbsp; &nbsp; &nbsp; // process data&nbsp; &nbsp; &nbsp; &nbsp; return false; // break if needed&nbsp; &nbsp; }});使用 Eloquent 也可以实现相同的效果。User::chunk(100, function ($users) {&nbsp; &nbsp; ...}
随时随地看视频慕课网APP
我要回答