Stop Memory Issues in Laravel Using chunkById()
The Problem: Loading Too Much Data
In real Laravel projects, you sometimes need to process thousands or even millions of records.
Example tasks:
- Sending bulk notifications
- Updating product prices
- Processing analytics data
If you run something like:
$orders = Order::all();
Laravel loads all records into memory, which can easily crash your server.
The Better Solution: chunkById()
Laravel provides chunkById() to process records in small batches.
Order::chunkById(100, function ($orders) {
foreach ($orders as $order) {
// Process each order
}
});
What happens here:
- Only 100 records are loaded at a time
- Memory usage stays low
- The script can process huge datasets safely
Why chunkById() Is Better Than chunk()
chunk() can break if records are updated during processing.
chunkById() avoids this problem by always moving forward using the primary key.
This makes it safer for updates and deletes.
When You Should Use chunkById()
Use it when:
- Processing large tables
- Running background jobs
- Updating many records
- Handling data migrations