maatwebsite/excel
Laravel Excel is a Laravel wrapper around PhpSpreadsheet for fast, elegant Excel/CSV exports and imports. Export collections or queries with automatic chunking for performance, generate downloadable files, and process imports cleanly in your app.
FromModel, FromCollection, FromView).WithValidation, ShouldQueue, WithStyles) allow granular adoption. Ideal for microservice-like architecture where only specific functionalities are needed.BeforeImport, AfterImport, Failed) for observability and extensibility (e.g., logging, notifications).maatwebsite/excel) + optional config publishing. No major framework modifications required.Excel::download()) and CLI imports/exports via Artisan commands.assertExported, assertImported) and mocking utilities reduce QA overhead.1.16 in v3.1.28) may conflict with other packages. Test thoroughly for compatibility.WithCalculatedFormulas, HasReferencesToOtherSheets) add cognitive load. Risk of misconfiguration in edge cases (e.g., circular references, nested formulas).QueueExport, QueueImport) depend on Laravel’s queue system. Failures may require manual intervention (e.g., retry logic, dead-letter queues).WithValidation), or is bulk insert sufficient?Laravel Excel handle migrations?// Export a model to Excel (queued)
User::chunk(1000, function ($users) {
Excel::queue(new UsersExport($users))->onQueue('excel');
});
FromModel, FromCollection) to validate basic functionality.Excel::download() for immediate feedback and Excel::store() for background processing.WithValidation, ShouldQueue) as needed.WithStyles for branded reports after core functionality is stable.FromView to render Blade templates to Excel..xls), ensure PhpSpreadsheet compatibility or implement a conversion layer.strict_types compatibility if using older PHP.composer require maatwebsite/excel.php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider".config/excel.php (e.g., disk settings, chunk size).app/Exports/UsersExport.php).use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
class UsersExport implements FromCollection, WithHeadings {
public function collection() { return User::all(); }
public function headings(): array { return ['Name', 'Email']; }
}
Excel::fake() for mocking).failed_jobs table).chunkSize in config) based on performance metrics.Excel::store() with TTL).composer why-not maatwebsite/excel to check for conflicts.config/excel.php) reduces drift but requires coordination across teams.SkipsEmptyRows was added in v3.1.27.memory_limit in php.ini for large files (e.g., 512M).WithValidation).Excel::store() with disk: local for debugging temporary files.config/excel.php:
'log' => true,
'log_level' => 'debug',
chunkSize: 500).league/csv) as a fallback.chunk() or cursor() for database queries to avoid loading all data into memory.User::cursor()->each(function ($user) {
// Process in stream
});
| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Queue worker crashes | Stalled exports/imports | Implement retry logic (e.g., retry_after in config). |
| Memory exhaustion | Job timeouts | Increase memory_limit, reduce chunk size, or use streaming. |
| Malformed Excel file | Corrupted data | Validate files before processing (e.g., WithValidation). |
| Database connection issues | Failed imports | Use transactions (WithTransactions) and retry failed rows. |
| PhpSpread |
How can I help you explore Laravel packages today?