Laravel Excel
Use this skill when a Laravel task involves spreadsheet exports, imports, queued spreadsheet processing, CSV files, or package maintenance for maatwebsite/excel.
Read First
Use search-docs when it is available for Laravel framework integration details. For package-specific behavior in this repository, inspect:
src/Excel.phpsrc/Exporter.phpsrc/Importer.phpsrc/Concerns/src/Fakes/ExcelFake.phpconfig/excel.phptests/Concerns/tests/ExcelFakeTest.php../../examples/exports-imports.md../../references/package.md
If the user asks for complete coverage, "all features", package docs, package skills, or a missing-feature audit, read ../../references/package.md first and use it as the checklist.
Working Rules
- Keep controllers thin. Put spreadsheet behavior in dedicated export/import classes.
- Use package concerns to declare behavior instead of custom imperative spreadsheet code.
- Before claiming feature completeness, compare the output against
../../references/package.md. - Use
Exportableon export classes when the export should calldownload(),store(),queue(), orraw()directly. - Use
Importableon import classes when the import should callimport(),queue(),toArray(), ortoCollection()directly. - Prefer
FromQueryfor large exports so the package can chunk records efficiently. - Prefer
WithChunkReadingplusWithBatchInsertsfor largeToModelimports. - Add Laravel's
Illuminate\Contracts\Queue\ShouldQueueto exports/imports that may exceed request time limits. - Use
WithMultipleSheetsfor workbooks with multiple worksheets, and keep each sheet as its own export/import object. - Use
WithHeadingRowwhen import files have user-facing headers; validate using heading keys instead of numeric indexes. - Use
WithValidation,SkipsOnFailure,SkipsFailures,SkipsOnError, andSkipsErrorsdeliberately when importing untrusted user files. - Use
WithCustomCsvSettingsfor delimiter, enclosure, input encoding, or BOM-sensitive CSV work. - Use PhpSpreadsheet concerns and events only when package concerns do not cover the required formatting or worksheet behavior.
- Do not load large spreadsheets fully into memory unless the task explicitly needs complete workbook inspection.
Feature Coverage Checklist
Use ../../references/package.md for the complete matrix. It covers:
- Facade APIs, object traits, contracts, file type constants, and extension detection.
- Export sources, export formatting, events, styles, formulas, charts, drawings, properties, multiple sheets, conditional sheets, and CSV settings.
- Import targets, heading rows, grouped headings, mapped cells, read filters, row/column limits, formulas, formatting, empty rows, unknown sheets, row numbers, and chunk offsets.
- Validation, failures, errors, batch inserts, upserts, duplicate handling, relation persistence, queues, batches, and queue-without-chain behavior.
- Console generators, published stubs/config, collection/query macros, cache, transactions, temporary files, remote temp disks, value binders, cell middleware, fakes, and package test locations.
Common API
Exports:
Excel::download($export, 'file.xlsx')Excel::store($export, 'exports/file.xlsx', 'disk')Excel::queue($export, 'exports/file.xlsx', 'disk')Excel::raw($export, Excel::XLSX)
Imports:
Excel::import($import, $filePath, $disk)Excel::queueImport($import, $filePath, $disk)Excel::toArray($import, $filePath, $disk)Excel::toCollection($import, $filePath, $disk)
Writer and reader types are usually detected from the file extension. Pass explicit types such as Excel::XLSX or Excel::CSV when the extension is absent or ambiguous.
Export Pattern
Start with the smallest concern set that matches the source:
FromCollectionfor an existing collection.FromArrayfor simple arrays.FromGeneratororFromIteratorfor streamed in-memory rows.FromQueryfor Eloquent or query builder datasets.FromViewwhen a Blade table is the clearest representation.
Add output concerns only when needed:
WithHeadingsfor header rows.WithMappingfor row transformation.WithColumnFormattingfor Excel number/date formats.ShouldAutoSizeorWithColumnWidthsfor layout.WithStyles,WithDefaultStyles,WithBackgroundColor, orWithEventsfor workbook styling.
Import Pattern
Choose the import target deliberately:
ToModelto persist rows as Eloquent models.ToCollectionfor application-level processing.ToArrayfor lightweight read-only extraction.OnEachRowfor row-by-row custom handling.
For production user uploads, include validation and failure behavior instead of assuming perfect files. Use chunk reading for large files and avoid cross-row state that cannot survive queued chunk jobs.
Queues And Batches
- If an export/import implements
ShouldQueue,store()andimport()may dispatch queued work. - Use
queue()orqueueImport()when queueing should be explicit at the call site. - Use
ShouldBatchwhen the export/import should return a Laravel pending batch. - Use
ShouldQueueWithoutChainonly when queued import chunks should not be chained. - Keep queued export/import classes serializable. Avoid closures, open file handles, or non-serializable service instances on properties.
Testing
- Use
Excel::fake()when testing that a controller, command, job, or service requested the correct spreadsheet operation. - Assert intent with
assertDownloaded(),assertStored(),assertQueued(),assertImported(), andassertExportedInRaw(). - Use callback assertions to verify the export/import object class and constructor state.
- Generate and read real spreadsheet files only when testing cell values, multiple sheets, formatting, formulas, or PhpSpreadsheet compatibility.
- In this package repository, follow existing PHPUnit tests under
tests/Concerns/,tests/ExcelFakeTest.php, andtests/Queued*Test.php.
Maintenance Notes
- Preserve Laravel auto-discovery entries in
composer.jsonfor the service provider andExcelfacade alias. - Keep config changes in
config/excel.phpbackward compatible unless the task explicitly targets a breaking release. - When changing a concern, update or add tests near the matching
tests/Concerns/*Test.phpfile. - When changing fakes, update
tests/ExcelFakeTest.php. - When changing queue behavior, update the matching queued import/export tests.