Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stefanledin's avatar

Memory issue: Loop over a large array of models with nested relationships

I’m working on a function that exports an array of customers to an excel file. It’s about 8 000 customers and I’m running the function as a Job.

However, I keep getting either Maximum execution time exceeded or Allowed memory size of bytes exhausted errors.

I’ve tinkered around quite a lot with lazy collections and/or $collection->chunk() without any improvements. I think I know where the bottlenecks are, but it’s still data that I need in the report.

The thing is, it’s not just a plain list of customers. They all have a few relationships and some of them is nested. For example:

$customer->services
$customer->advisors->roles 
$customer->contacts // Phone, email [...]

So when I'm looping over the customers, I need to process the data. Create a list of the services names, filter the advisors depending on their roles, find and place the email addresses and phone numbers in the correct "column" and things like that.

// Simplified example:
$customers = Customer::with(['services', 'advisors.roles', 'contacts'])->get();
$customerReport = collect([]);

foreach($customers->chunk(30) as $chunk) {
  foreach($chunk as $customer) {
    $customerReport->push([
      $customer->name,
      $customer->address,
      $customer->services->pluck('name')->implode(', '),
    ]);
  }
}

// I'm out of memory or the execution time has been exceeded before this:
createExcelFile($customerReport);

How would you do this in a more efficient way? I guess that the $customerReport array grows too big? I'm thinking about creating a temporary database table and insert data into that instead of creating a large array in memory?

Any help would be much appreciated :)

0 likes
2 replies
jlrdw's avatar

I used to dump data to local, and via an odbc connection run monthly accounts receivable and accounts payable reports in Microsoft Access. Prior to access I used visual foxpro.

Libre office base could work, there is also jasper reports. There is also Crystal Reports, but I never use that.

But 8000 should not be using up memory that bad. How many with nesting.

If this is to view on screen only you could double paginate, in other words have, just using accounts receivable for an example:

Span with company, with next link.

Underneath a paginated of list that company's receivables.

Just adapt to your data.

1 like

Please or to participate in this conversation.