I am in processes to generate an array of numbers from available starting and ending numbers but I am facing Allowed memory size exhausted error while generating 1 million records. I do not want to store any information in the memory. So, Please review and guide that what is the issue or how I can iterate the code below to achieve this.
$start = "0";
//$end = "5000";
$end = "16777216";
for ($i = $start; $i <= $end; $i++) {
$range[] = "Number $i";
}
//$total=count($range);
$perPage = 25;
$currentPage = request()->input("page") ?? 1;
$startingPoint = ($currentPage * $perPage) - $perPage;
$paginatedItems = array_slice($range, $startingPoint, $perPage, true);
$paginated = new Paginator(
$paginatedItems,
count($range),
$perPage,
$currentPage,
['path' => request()->url(), 'query' => request()->query()]
);
return $paginated;
This code works well and generate the required results and then put these results in Laravel 9 Manual Pagination but when I try to generate a million or more records, it through the PHP default expection which I want to avoid based on the offset. Thank You