Each client application that we build eventually needs an export. First it is one CSV file. Then the client asks for Excel with frozen headings, for a PowerPoint deck, and for exports that take ten minutes. Then they ask where the file from last week is.
We built export code in many projects. Now it is a package: Laravel Export Engine.
What the package does
Laravel Export Engine makes files from your data. It keeps a record of each export, from the request to the download.
- Each export has a clear status: requested, generating, available, failed or expired.
- Exports run on the queue or in the request.
SpreadsheetExporterwrites CSV and Excel files with headers, frozen panes, column widths and more than one sheet.- Any class that implements the
Exportercontract can make a file, for example a PowerPoint deck. - An exporter can ask the person for settings before the export starts.
- The engine moves the file to a disk of your choice, and deletes it when it expires.
- It gives a download route, a policy and notifications. You can use them or replace them.
The engine has no views and no components. Your application shows the forms, the lists and the notifications.
Install the package
composer require bernskiold/laravel-export-engine
Publish the config and the migration. Then run the migration.
php artisan vendor:publish --tag=export-engine-config
php artisan vendor:publish --tag=export-engine-migrations
php artisan migrate
Schedule the sweep command. It expires old files and fails exports that nobody started.
Schedule::command('exports:sweep')->hourly();
Write an exporter
A spreadsheet exporter writes its rows in data(). The export gives the filters, the options and the format.
use Bernskiold\ExportEngine\Contracts\WithHeader;
use Bernskiold\ExportEngine\SpreadsheetExporter;
use Spatie\SimpleExcel\SimpleExcelWriter;
class BrandsExporter extends SpreadsheetExporter implements WithHeader
{
public function header(): array
{
return ['Brand', 'Rank'];
}
protected function data(SimpleExcelWriter $writer): void
{
Brand::query()
->whereIn('id', $this->export->filter('brands'))
->each(fn (Brand $brand) => $writer->addRow([
'Brand' => $brand->name,
'Rank' => $brand->rank,
]));
}
}
Add contracts for more control. For example, FreezesPanes freezes rows and columns, and SizesColumns sets the column widths. RequiresAbility lets only a person with a Gate ability use the exporter.
Start an export
Use a PendingExport:
use Bernskiold\ExportEngine\Data\PendingExport;
$export = (new PendingExport(
exporter: BrandsExporter::class,
downloadName: 'Brand Overview',
format: 'xlsx',
filters: ['brands' => [1, 2, 3]],
queued: true,
userId: auth()->id(),
))->start();
A queued export sends a notification when the file is ready or when the export fails. An export that runs in the request is available when start() returns. Send the file with $export->download().
The file name tells what the export is, its filters and when the engine made it. For example: "Brand Overview - Nike, Adidas +1 - UK 2025 - 2026-10-25 09-30.xlsx".
Ask for settings
An exporter can ask how the person wants the file. Declare the settings on the exporter:
public static function exportSettings(ExportSettingsContext $context): array
{
return [
ChoiceSetting::make('sheets', __('Sheets'))
->options(SheetLayout::class)
->default(SheetLayout::Combined)
->onlyForFormats('xlsx'),
ToggleSetting::make('totals', __('Totals'))->default(true),
];
}
Before the export starts, the engine holds it in the cache and gives you a token. Your form uses the token to get the settings, the start values and the validation rules. The start values can come from the last choices of the person. When the person submits the form, you start the export with the chosen values.
Run as the person who asked
A queued export has no signed-in user. Middleware wraps each export while it runs. Use it to set the locale, or to run the queries on behalf of the person who asked.
ExportEngine::middleware([RunAsRequester::class]);
If a person loses the Gate ability before a queued export runs, the export fails.
Downloads and notifications
Register the download route in a route group:
Route::middleware(['web', 'auth'])->group(fn () => ExportEngine::routes());
The default policy lets only the owner see and download an export. You can set your own policy and your own notifications in the config.
Test your exporters
The InteractsWithExports trait gives test helpers. runExportAsWorker() runs an export with no signed-in user, the same as a queue worker. This finds queries that use auth()->user() in place of the person who asked for the export.
A limit to know
An exporter always writes the file to a local staging disk first, because the spreadsheet and presentation writers need a real path. Then the engine moves the file to the export disk, which can be S3. The sweep deletes old files in the staging directory. Thus, use a directory that only the engine uses.
Get the package
Laravel Export Engine is open source, with the MIT license. The code and the full documentation are on GitHub. If you find a problem or have an idea, open an issue. We are happy to hear from you.