Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 11d3e98

Browse files
committed
Added 'chunk' method.
Added 'chunk' method to query builder and Eloquent for doing work on large result sets.
1 parent 9857193 commit 11d3e98

3 files changed

Lines changed: 50 additions & 1 deletion

File tree

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,30 @@ public function pluck($column)
136136
if ($result) return $result->{$column};
137137
}
138138

139+
/**
140+
* Chunk the results of the query.
141+
*
142+
* @param int $count
143+
* @param callable $callback
144+
* @return void
145+
*/
146+
public function chunk($count, $callback)
147+
{
148+
$results = $this->forPage($page = 1, $count)->get();
149+
150+
while (count($results) > 0)
151+
{
152+
// On each chunk result set, we will pass them to the callback and then let the
153+
// developer take care of everything within the callback, which allows us to
154+
// keep the memory low for spinning through large result sets for working.
155+
call_user_func($callback, $results);
156+
157+
$page++;
158+
159+
$results = $this->forPage($page, $count)->get();
160+
}
161+
}
162+
139163
/**
140164
* Get an array with the values of a given column.
141165
*

src/Illuminate/Database/Query/Builder.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,30 @@ protected function getCacheCallback($columns)
10741074
return function() use ($me, $columns) { return $me->getFresh($columns); };
10751075
}
10761076

1077+
/**
1078+
* Chunk the results of the query.
1079+
*
1080+
* @param int $count
1081+
* @param callable $callback
1082+
* @return void
1083+
*/
1084+
public function chunk($count, $callback)
1085+
{
1086+
$results = $this->forPage($page = 1, $count)->get();
1087+
1088+
while (count($results) > 0)
1089+
{
1090+
// On each chunk result set, we will pass them to the callback and then let the
1091+
// developer take care of everything within the callback, which allows us to
1092+
// keep the memory low for spinning through large result sets for working.
1093+
call_user_func($callback, $results);
1094+
1095+
$page++;
1096+
1097+
$results = $this->forPage($page, $count)->get();
1098+
}
1099+
}
1100+
10771101
/**
10781102
* Get an array with the values of a given column.
10791103
*

src/Illuminate/Foundation/changes.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
{"message": "Allow connection to be configurable when using Redis based sessions.", "backport": null},
3535
{"message": "Allow passing DateTime objects to Queue::later.", "backport": null},
3636
{"message": "Added Queue::bulk method for pushing several jobs out at once.", "backport": null},
37-
{"message": "Added 'dates' property to Eloquent model for convenient setting of date columns.", "backport": null}
37+
{"message": "Added 'dates' property to Eloquent model for convenient setting of date columns.", "backport": null},
38+
{"message": "Added 'chunk' method to query builder and Eloquent for doing work on large result sets.", "backport": null}
3839
]
3940
}

0 commit comments

Comments
 (0)