PHP

Efficiently Process Large Datasets with Eloquent `chunk`

Learn to process thousands of Eloquent records efficiently using the `chunk` method, reducing memory usage and preventing timeouts in Laravel applications.

use App\Models\User;

// Process users in chunks of 1000
User::chunk(1000, function ($users) {
    foreach ($users as $user) {
        // Perform operations on each user
        $user->update(['status' => 'processed']);
        // Example: dispatch(new ProcessUserJob($user));
    }
});

// For even larger datasets, consider `lazyById` (or `chunkById` in older versions)
// to ensure consistent iteration when records are updated/deleted during processing.
User::lazyById(1000, function ($users) {
    foreach ($users as $user) {
        // Perform operations on each user
        $user->update(['last_processed_at' => now()]);
    }
});
How it works: The `chunk` method retrieves a subset of database records at a time, passing each chunk to a given callback. This is ideal for processing large datasets without exhausting memory, as only a small portion of records are loaded into memory simultaneously. `lazyById` (or `chunkById` in older Laravel versions) is similar but more robust for iterating over large datasets where records might be added or deleted during the iteration, ensuring stable pagination by ID.

Need help integrating this into your project?

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

Hire DigitalCodeLabs