export large data with chunk in Laravel-Excel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Exporting Large Datasets Efficiently with Chunking in Laravel Excel

Exporting data from your database into a spreadsheet using packages like Laravel Excel is incredibly convenient. However, when dealing with very large datasets—say, thousands or tens of thousands of records—loading the entire result set into memory at once can lead to slow performance, high memory consumption, and potential timeouts. This is where the concept of chunking data becomes essential for robust data handling.

The initial example you provided works perfectly for small models because loading all records into memory is negligible. However, when you encounter large tables (like 9000+ records), we must switch from a monolithic query to an iterative approach using chunk().

Why Chunking is Necessary for Large Exports

When you execute a standard Eloquent query like $model->all(), Laravel fetches every single record into PHP memory before the export process begins. For massive datasets, this consumes excessive memory and can crash your application or server.

Chunking solves this by instructing the database to return results in manageable batches. Instead of fetching 9000 records simultaneously, you fetch them in smaller groups (e.g., 100 records at a time). This significantly reduces the memory footprint during the export process, making your operation stable and fast.

Implementing Chunking with Laravel Excel

The key to successfully combining chunking with Laravel-Excel lies in placing the chunking logic inside the closure provided to Excel::create(). This allows you to iterate over each batch and create a separate sheet for that batch, or append data correctly across multiple iterations.

Here is how you can adapt your approach to handle large exports efficiently:

use Maatwebsite\Excel\Facades\Excel;
use App\Models\MyModel; // Assuming MyModel is your Eloquent model

// Define the file name
$fileName = 'large_export_' . time() . '.xls';

try {
    Excel::create($fileName, function ($excel) use ($fileName) {
        
        // Use chunk() to iterate over the results in manageable batches (e.g., 100 records)
        MyModel::chunk(100, function ($models) use ($excel) {
            
            // Inside this closure, $models is the current chunk of data (an Eloquent Collection)
            
            // Create a new sheet for each chunk to maintain separation
            $excel->sheet('Data_Chunk_' . $excel->sheetCount(), function ($sheet) use ($models) {
                
                // Use fromModel or fromArray based on your needs. 
                // fromModel is often cleaner when dealing with Eloquent collections.
                $sheet->fromModel($models, null, 'A1', true);
            });
        });

    })->export('xls');

} catch (\Exception $e) {
    // Handle any exceptions during the export process
    echo "Error during export: " . $e->getMessage();
    return false;
}

Resolving Sheet Order and File Structure Confusion

Your confusion regarding sheet order and file structure is common when dealing with iterative exports. The solution lies in understanding the scope of the iteration:

  1. Scope: When you use chunk(), the code inside the callback runs once for every batch returned by the database.
  2. Sheet Creation: By calling $excel->sheet(...) inside the chunk loop, you are dynamically creating a new sheet for every batch processed. This ensures that your resulting Excel file contains multiple sheets, one for each chunk of data, which is excellent for large exports where separating the data logically is beneficial.
  3. File Generation: The entire operation—the Excel::create() call and the final export('xls')—happens only after all chunks have been processed successfully. This guarantees that the file is built correctly, sheet by sheet, in the correct sequence determined by the iteration.

Remember that efficient data handling is crucial when scaling your application. For deep dives into optimizing database queries and Eloquent relationships, exploring resources from official sources like laravelcompany.com can provide excellent context for building high-performance backends.

Conclusion

By integrating the chunk() method within the Excel::create() closure, you effectively transform a memory-intensive operation into an iterative, memory-efficient process. This technique allows you to reliably export massive datasets without crashing your server, ensuring that even the largest exports are handled gracefully by Laravel Excel. Use chunking for scale, and enjoy smoother data operations!