0
0
LaravelHow-ToBeginner · 3 min read

How to Use Chunk in Eloquent for Efficient Data Processing

Use the chunk method on an Eloquent query to process large datasets in smaller pieces. It accepts a chunk size and a callback function that handles each chunk, helping avoid memory overload.
📐

Syntax

The chunk method takes two main parts:

  • Chunk size: Number of records to fetch per batch.
  • Callback function: A function that receives each chunk as a collection to process.

This method breaks the query results into smaller groups to handle large data efficiently.

php
Model::chunk(int $count, Closure $callback): bool
💻

Example

This example shows how to process users in chunks of 100 to send an email to each without loading all users at once.

php
<?php
use App\Models\User;

User::chunk(100, function ($users) {
    foreach ($users as $user) {
        // Example action: send email
        echo "Sending email to: {$user->email}\n";
    }
});
Output
Sending email to: user1@example.com Sending email to: user2@example.com ... (continues for each user in chunks of 100)
⚠️

Common Pitfalls

Common mistakes when using chunk include:

  • Modifying the query inside the callback, which can cause unexpected behavior.
  • Not returning false from the callback to stop processing early if needed.
  • Using get() inside the callback, which defeats the purpose of chunking.

Always process data inside the callback without re-querying.

php
<?php
// Wrong: calling get() inside chunk callback
User::chunk(100, function ($users) {
    $allUsers = User::get(); // Avoid this
});

// Right: process $users directly
User::chunk(100, function ($users) {
    foreach ($users as $user) {
        // process user
    }
});
📊

Quick Reference

MethodDescription
chunk($count, $callback)Process records in chunks of $count, calling $callback for each chunk.
return false in callbackStop chunking early if condition met.
Use inside Eloquent query builderWorks with any Eloquent model query.

Key Takeaways

Use chunk to process large datasets in small parts to save memory.
Pass the chunk size and a callback to handle each batch of records.
Avoid querying inside the callback; process the given chunk directly.
Return false from the callback to stop processing early if needed.
Works with any Eloquent model query for efficient data handling.