@@ -112,6 +112,35 @@ public function each(callable $callback, $count = 1000)
112
112
* @return bool
113
113
*/
114
114
public function chunkById ($ count , callable $ callback , $ column = null , $ alias = null )
115
+ {
116
+ return $ this ->orderedChunkById ($ count , $ callback , $ column , $ alias );
117
+ }
118
+
119
+ /**
120
+ * Chunk the results of a query by comparing IDs in descending order.
121
+ *
122
+ * @param int $count
123
+ * @param callable $callback
124
+ * @param string|null $column
125
+ * @param string|null $alias
126
+ * @return bool
127
+ */
128
+ public function chunkByIdDesc ($ count , callable $ callback , $ column = null , $ alias = null )
129
+ {
130
+ return $ this ->orderedChunkById ($ count , $ callback , $ column , $ alias , true );
131
+ }
132
+
133
+ /**
134
+ * Chunk the results of a query by comparing IDs in a given order.
135
+ *
136
+ * @param int $count
137
+ * @param callable $callback
138
+ * @param string|null $column
139
+ * @param string|null $alias
140
+ * @param bool $descending
141
+ * @return bool
142
+ */
143
+ public function orderedChunkById ($ count , callable $ callback , $ column = null , $ alias = null , $ descending = false )
115
144
{
116
145
$ column ??= $ this ->defaultKeyName ();
117
146
@@ -127,7 +156,11 @@ public function chunkById($count, callable $callback, $column = null, $alias = n
127
156
// We'll execute the query for the given page and get the results. If there are
128
157
// no results we can just break and return from here. When there are results
129
158
// we will call the callback with the current chunk of these results here.
130
- $ results = $ clone ->forPageAfterId ($ count , $ lastId , $ column )->get ();
159
+ if ($ descending ){
160
+ $ results = $ clone ->forPageBeforeId ($ count , $ lastId , $ column )->get ();
161
+ } else {
162
+ $ results = $ clone ->forPageAfterId ($ count , $ lastId , $ column )->get ();
163
+ }
131
164
132
165
$ countResults = $ results ->count ();
133
166
0 commit comments