File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 *
Original file line number Diff line number Diff 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 *
Original file line number Diff line number Diff line change 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}
You can’t perform that action at this time.
0 commit comments