PHP

Process Large Datasets in Chunks

Learn to process thousands of database records efficiently using Eloquent's `chunk` or `chunkById` methods, preventing memory exhaustion when dealing with very large datasets in Laravel.

use App\Models\Order;

// Process orders in chunks of 500 records.
Order::where('status', 'pending')
    ->chunk(500, function ($orders) {
        foreach ($orders as $order) {
            // Perform actions on each order
            $order->process();
            $order->save();
        }
    });

// For even better performance on very large tables, use chunkById.
// This method automatically handles offsets and limits based on the primary key.
Order::where('status', 'completed')
    ->chunkById(1000, function ($orders) {
        foreach ($orders as $order) {
            // Perform actions on each order
            $order->generateInvoice();
            $order->save();
        }
    }, $column = 'id', $alias = null);
How it works: When dealing with a very large number of database records, retrieving all of them at once can consume significant memory. Eloquent's `chunk` method retrieves a subset (chunk) of results at a time and feeds them to a closure, allowing you to process records without loading the entire dataset into memory. `chunkById` is generally preferred for very large tables as it uses the primary key to determine the next chunk, which can be more efficient than `chunk` for pagination especially with complex `ORDER BY` clauses.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs