Number::pairs()
Split a numeric range into evenly sized chunks as start/end pairs — great for batch processing and pagination math.
Overview
Number::pairs() takes a total and a chunk size, and returns an array of [start, end] pairs. It handles the math of splitting ranges so you don't have to.
Usage
use Illuminate\Support\Number;
Number::pairs(25, 10);
// [[0, 10], [10, 20], [20, 25]]
You can also provide an offset:
Number::pairs(25, 10, offset: 100);
// [[100, 110], [110, 120], [120, 125]]
Real-World Example
Batch-processing records by ID range:
$max = User::max('id');
foreach (Number::pairs($max, 1000) as [$start, $end]) {
User::whereBetween('id', [$start, $end])
->chunk(100, function ($users) {
// Process chunk...
});
}
When to Use
- Splitting work into parallel batches for queue jobs
- Manual pagination or range-based queries
- Distributing ID ranges across workers