Almost every client application we build has an import. The data comes from a spreadsheet that a person made by hand. The headings change, the dates have different formats, and some cells contain "n/a". After the import, the person asks: "What happened to my rows?"
We solved these problems many times. Now the solution is a package: Laravel Import Engine.
What the package does
You write one class for each type of import. The class tells which columns it reads and what to do with one row. The engine does the other work.
- It reads the file with tolerance. It finds the headings under a title row, it reads semicolon files, and it finds the text encoding.
- It guesses the column map from the headings. It also accepts a map from a person.
- It makes a preview. It runs the import in a transaction, and then it rolls the transaction back.
- It runs the import on the queue, as the person who started it.
- It keeps a report with one entry for each row: created, updated, unchanged, conflicted, skipped or failed.
Install the package
composer require bernskiold/laravel-import-engine
Publish the migration and the configuration file. Then run the migration.
php artisan vendor:publish --tag=import-engine-migrations
php artisan vendor:publish --tag=import-engine-config
php artisan migrate
The engine reads .csv, .txt and .xlsx files.
Write an importer
Extend Importer. Give the columns in columns(), and write importRow().
use Bernskiold\LaravelImportEngine\Importer;
use Bernskiold\LaravelImportEngine\Support\Column;
use Bernskiold\LaravelImportEngine\Support\ImportReport;
use Bernskiold\LaravelImportEngine\Support\ImportRow;
class ContactImporter extends Importer
{
public function columns(): array
{
return [
Column::make('client', __('Client'))->required()->aliases('client name', 'account'),
Column::make('email', __('Email'))->required()->aliases('email address', 'e-mail'),
Column::make('name', __('Name')),
];
}
protected function importRow(ImportRow $row, ImportReport $report): void
{
$client = Client::query()->firstWhere('name', $row->get('client'));
if ($client === null) {
$report->conflicted($row, $row->get('email'), [__('No client has this name.')]);
return;
}
$contact = $client->contacts()->firstOrNew(['email' => $row->get('email')]);
$contact->fill(array_filter(['name' => $row->get('name')]));
$created = ! $contact->exists;
$changed = $contact->isDirty();
$contact->save();
match (true) {
$created => $report->created($row, $contact->email),
$changed => $report->updated($row, $contact->email),
default => $report->unchanged($row, $contact->email),
};
}
}
Record one entry for each row. Use unchanged() when the record already had the values of the row. Then a second run of the same file shows that nothing changed.
Each row is in its own database transaction. If a row fails, the engine rolls back the writes of that row only, and it reads the next row.
Then add the importer to a string-backed enum of your import types. Set the enum in config/import-engine.php.
Read the values of a row
ImportRow gives tolerant access to the values. Each value is a trimmed string. Placeholders such as -, n/a, TBC and #N/A become null.
$row->get('email');
$row->integer('rank'); // "#4" and "4.0" are 4
$row->boolean('is_active'); // "yes", "y", "x" and "1" are true
$row->date('sent'); // "25/10/2026" and "25 Oct 2026" are dates
$row->values('tags'); // splits on commas, semicolons and line breaks
You can also give Laravel validation rules in rules(). The engine validates each row before importRow(). A row that is not valid is a failed entry, with the messages of the validator.
Run an import
StartImport keeps the file on a disk, makes the import record and sends the run to the queue:
use Bernskiold\LaravelImportEngine\Actions\StartImport;
$import = app(StartImport::class)->execute(
type: ImportType::Contacts,
file: $request->file('file'),
map: ['Client Name' => 'client', 'E-mail' => 'email'],
);
An empty map tells the engine to guess the map from the headings.
The queued job signs in the person who started the import. Thus activity logs and "created by" columns get the correct user. During the run, the record keeps the progress. A screen can poll $import->progress().
Each record runs one time. If the queue delivers the job two times, the second job does nothing.
Make a preview
PreviewImport runs the import in a transaction, and then rolls the transaction back. Thus the preview shows what the run will do. It is not a prediction.
use Bernskiold\LaravelImportEngine\Actions\PreviewImport;
$report = app(PreviewImport::class)->execute(ImportType::Contacts, $path, $map, limit: 500);
Read the report
$report = $import->importReport();
$report->countOf(ImportOutcome::Created); // 412
$report->needingAttention(); // the entries that are not a success
$report->changedNothing(); // true when all entries are unchanged
Each entry has the line number in the file. Thus a person can find the row in the spreadsheet. Conflicted is not a failure. It means that the file does not agree with your data, and the importer did not change the data.
Build the screens
The package has no views. It gives the data that a mapping screen needs: the headings of the file, the first rows, the columns of the importer and the guessed map. It can also make an Excel template from the columns. A completed template then maps itself.
A limit to know
The preview rolls back the database only. It does not undo an email or a queued job. In the importer, call previewing() to stop these side effects during a preview.
if (! $this->previewing()) {
Mail::to($contact)->send(new Welcome($contact));
}
The package also has no policies. Your application must validate the upload, check who can start an import and decide how long to keep the reports.
Get the package
Laravel Import Engine is open source, with the MIT license. The code and the full documentation are on GitHub. The documentation also shows simple importers, events, pruning and test helpers. If you find a problem or have an idea, open an issue. We are happy to hear from you.