@normatov13 The lazy method will query “chunks” of rows at a time, and then return that “chunk” as a collection of Eloquent records.
Article::query()->chunkById(function ($articles) {
foreach ($articles as $article) {
// Do something with article in chunk...
}
});
The cursor method does a single database query, but will only convert one row at a time to an Eloquent model:
$articles = Article::query()->cursor();
foreach ($articles as $article) {
// Do something with article...
}
When iterating over large data sets I prefer to use the chunk approach. It just makes more sense to load say, 1000 rows at a time, iterate over those, and then load the next 1000. With the cursor approach, if you have a large data set, then it’s still going to pull back all rows, and they still need holding in memory somewhere. If you have say, millions of rows, this could cause problems.