Installation
composer require 2lenet/import-bundle
(Note: The README mentions composer update, but require is the modern standard.)
Register the Bundle
Add to config/bundles.php (Laravel 5.5+):
ClickAndMortar\ImportBundle\ClickAndMortarImportBundle::class,
Configure a Basic Import
Define in config/import.php (create if missing):
'entities' => [
'customer_from_csv' => [
'model' => App\Models\Customer::class,
'unique_key' => 'id',
'mappings' => [
'id' => 'ID',
'name' => 'FullName',
'email' => 'EmailAddress',
],
],
],
First Use Case Create a controller method to handle file uploads:
use ClickAndMortar\ImportBundle\Service\ImportService;
public function import(Request $request, ImportService $importService)
{
$file = $request->file('csv_file');
$result = $importService->import('customer_from_csv', $file->getPathname());
return response()->json($result);
}
File Handling
Request to capture uploaded files:
$file = $request->validate([
'csv_file' => 'required|file|csv',
])->file('csv_file');
$file->openFile()->fpassthru();
Mapping Strategy
$importService->import('customer_from_csv', $filePath, [
'mappings' => [
'name' => 'CustomNameColumn',
],
]);
model relationships:
mappings:
address: "Address_Line1,Address_Line2"
(Assumes Customer has an address relationship.)Batch Processing
$importService->setChunkSize(100); // Process 100 records at a time
Event Listeners
event(new ImportStarting('customer_from_csv', $filePath));
$result = $importService->import(...);
event(new ImportCompleted($result));
ClickAndMortar\ImportBundle\Validator\ValidatorInterface for custom rules.\Log::info('Import started', ['entity' => 'customer_from_csv']);
dispatch(new ImportJob('customer_from_csv', $filePath));
(Create a job extending ShouldQueue.)Case Sensitivity
$headers = array_map('strtolower', $csvHeaders);
Unique Key Conflicts
unique_key exists in the DB, the bundle skips duplicates by default.overwrite_on_duplicate: true in config.Memory Limits
memory_limit.SimpleXML with libxml_disable_entity_loader(true).Entity Not Found
model class is invalid, the bundle throws a RuntimeException.config/import.php.Enable Verbose Logging
Add to config/import.php:
'debug' => env('APP_DEBUG', false),
Logs detailed mapping and validation steps.
Test with Small Files Use a 5-row CSV first to validate mappings before processing large datasets.
Custom Writers
Override ClickAndMortar\ImportBundle\Writer\WriterInterface to support new formats (e.g., JSON):
class JsonWriter implements WriterInterface {
public function write(array $data, string $filePath) { ... }
}
Pre/Post-Processors
Hook into ImportEvent listeners for data transformation:
public function onImportStarting(ImportStarting $event) {
$event->setData(array_map(function($row) {
return strtoupper($row['name']); // Example: Uppercase names
}, $event->getData()));
}
Database Transactions Wrap imports in a transaction for atomicity:
DB::transaction(function() use ($importService) {
$importService->import('customer_from_csv', $filePath);
});
Default Values The bundle assumes:
unique_key is a column in the DB.mappings use dot notation for nested attributes (e.g., address.city).XML Namespaces For XML imports, specify namespaces in the config:
click_and_mortar_import:
entities:
product_from_xml:
model: App\Models\Product::class
xml_namespace: "urn:example"
mappings:
sku: "/ns:product/ns:sku"
How can I help you explore Laravel packages today?