How to chunk results from a custom query in Laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Efficiently Chunking Results from Custom Queries in Laravel
Introduction:
Chunking large results sets can greatly improve the performance of your application by limiting the memory consumption and reducing query time. Laravel's Eloquent ORM has a built-in chunk() method to help you process large data sets with ease. In this blog post, we will guide you through implementing chunking on custom queries.
The Custom Query:
Let's assume that you have the following legacy database query that gets all the companies and maps them into corresponding models in your new system:
```php
$companies = DB::connection('legacy')->select("..."); // Sample query (real query not provided)
```
Implementing Chunking with Eloquent:
Now, you want to implement Laravel's chunk() method to process this data. The chunk() method can be used as follows in your model or controller:
```php
User::chunk(200, function($users) {
foreach ($users as $user) {
// Process each user record
}
});
```
However, to apply this solution to your custom query, you must adapt it. Firstly, replace the relation name (User) with DB::connection('legacy')->select() to fetch data from your legacy database:
```php
DB::connection('legacy')->select("SELECT * FROM companies")->chunk(200, function($companies) {
foreach ($companies as $company) {
// Process each company record
}
});
```
The above code block will iterate through each chunk of 200 companies and perform your custom processing. However, you may receive no response from this code because the chunk() method expects a callable closure or a callback function to be passed as the second argument. In your case, you have not defined any processing logic inside the foreach loop after the company data is fetched.
Incorporating Processing Logic:
To implement proper processing logic in this scenario, ensure that you're mapping legacy company data to a new system model or transforming the data as needed. For instance:
```php
DB::connection('legacy')->select("SELECT * FROM companies")->chunk(200, function($companies) {
foreach ($companies as $company) {
// Map or transform the company data to Eloquent model
$entity = Entity::firstOrNew(['external_id' => $company->companyKey]);
$entity->name = $company->companyName;
$entity->save();
}
});
```
With this code, you will have chunked the results and processed them efficiently. This approach will ensure that your application does not run out of memory or timeouts due to large datasets.
Conclusion:
Implementing chunking in Laravel can significantly improve the performance of applications that process large amounts of data. By properly adapting the Eloquent ORM's chunk() method, you can efficiently transfer and transform legacy data or any other information into your new system with ease.