itmosoft / laravel-import-export
Streaming CSV import and export utilities with header mapping, row validation, and error reporting for Laravel applications.
Requires
- php: ^8.3
- illuminate/contracts: ^12.0 || ^13.0
- illuminate/support: ^12.0 || ^13.0
Requires (Dev)
- orchestra/testbench: ^10.0 || ^11.0
- phpunit/phpunit: ^11.5 || ^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Streaming CSV import and export utilities with header mapping, row validation, and error reporting for Laravel applications.
Requirements
- PHP 8.3+
- Laravel 12 or 13
Installation
composer require itmosoft/laravel-import-export php artisan vendor:publish --tag=itmosoft-import-export-config
Laravel package auto-discovery registers the service provider.
Export CSV
use ItmoSoft\ImportExport\ImportExportManager; $manager = app(ImportExportManager::class); $count = $manager->exportCsv( storage_path('app/exports/users.csv'), User::query()->select(['name', 'email'])->cursor()->map(fn ($user) => [ 'name' => $user->name, 'email' => $user->email, ]), );
For predictable columns, pass explicit headers:
$manager->exportCsv($path, $rows, ['name', 'email']);
The exporter streams rows to disk instead of assembling the entire CSV in memory.
Import CSV
$result = $manager->importCsv( storage_path('app/imports/users.csv'), function (array $row, int $line): bool { validator($row, [ 'name' => ['required', 'string'], 'email' => ['required', 'email'], ])->validate(); User::create($row); return true; }, );
The callback returns:
true: row imported.false: row intentionally skipped.- Throw an exception: row skipped and an error is added to the result; processing continues.
Inspect the result:
$result->processed; $result->imported; $result->skipped; $result->errors; $result->hasErrors();
ImportError includes the CSV row number and exception message. Row numbering starts at the header row, so the first data record is row 2.
CSV options
Publish config/itmosoft-import-export.php to configure:
- delimiter
- enclosure
- escape
- UTF-8 BOM on exports
- skipping empty rows
The importer strips a UTF-8 BOM from the first header.
Scope boundaries
This release supports CSV only. It does not include XLS/XLSX, PDF, background jobs, database-specific import transactions, automatic model persistence, or file upload UI. Implement validation and persistence in the import callback.
Tests
composer install
composer test
License
MIT. See LICENSE.