Download the PHP package elachagui/laravel-smart-importer without Composer
On this page you can find all versions of the php package elachagui/laravel-smart-importer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download elachagui/laravel-smart-importer
More information about elachagui/laravel-smart-importer
Files in elachagui/laravel-smart-importer
Package laravel-smart-importer
Short Description A production-ready Laravel package for importing XLSX and CSV files into Eloquent models with column mapping, validation, duplicate handling, logging, chunking, and queue support.
License MIT
Informations about the package laravel-smart-importer
Laravel Smart Importer
A production-ready Laravel package for importing XLSX and CSV files into Eloquent models with column mapping, validation, duplicate handling, relational imports, logging, chunking, and queue support.
Features
- Multiple File Formats: Support for CSV, XLSX, and XLS files
- Dynamic Column Mapping: Map file columns to model attributes
- Relational Imports: Import data with all Laravel Eloquent relation types
- Validation: Validate each row using model-defined rules
- Duplicate Handling: Skip, update, or fail on duplicate records
- Chunking: Process large files in memory-efficient chunks
- Queue Support: Process imports asynchronously using Laravel queues
- Logging: Track import progress with detailed logs
- Events: Listen to import lifecycle events
- Field Transformers: Transform data before importing
- Preview: Preview file contents before importing
- Security: Whitelist allowed models for import
Requirements
- PHP 8.1+
- Laravel 10.0+
Installation
Install the package via Composer:
Publish the configuration file:
Run the migrations:
Configuration
The configuration file config/smart-importer.php allows you to customize:
Basic Usage
Simple Import
With Duplicate Handling
With Field Transformers
Queue Processing
With Options
Preview File
Get File Headers
Relational Imports
The package supports importing data with all Laravel Eloquent relation types. This allows you to create related records, look up existing records for foreign keys, and sync many-to-many relationships during import.
Supported Relation Types
| Relation Type | Description | Processing Phase |
|---|---|---|
belongsTo |
Lookup/create parent record, set foreign key | Before main model |
hasOne |
Create single child record | After main model |
hasMany |
Create multiple child records | After main model |
belongsToMany |
Sync pivot table (many-to-many) | After main model |
morphOne |
Polymorphic one-to-one | After main model |
morphMany |
Polymorphic one-to-many | After main model |
morphTo |
Inverse polymorphic (set type + id) | Before main model |
morphToMany |
Polymorphic many-to-many | After main model |
morphedByMany |
Inverse polymorphic many-to-many | After main model |
hasOneThrough |
Through intermediate model | After main model |
hasManyThrough |
Through intermediate model | After main model |
BelongsTo Relations
Look up an existing parent record (or create it if missing) and automatically set the foreign key.
Composite Key Lookup:
HasOne / HasMany Relations
Create child records after the main model is saved.
Multiple Records from Delimited Column:
Multiple Records from JSON Column:
BelongsToMany Relations (Many-to-Many)
Sync pivot table relationships from a column containing multiple values.
Sync Modes:
| Mode | Behavior |
|---|---|
sync |
Replace all existing relations with new ones |
attach |
Add new relations without removing existing |
toggle |
Toggle relations (add if missing, remove if present) |
syncWithoutDetaching |
Add new relations, keep existing ones |
Polymorphic Relations
MorphMany - Create polymorphic child records:
MorphTo - Set polymorphic parent:
MorphToMany - Polymorphic many-to-many:
Through Relations
Create records through an intermediate model.
Model-Defined Relations
Define relations directly in your model by implementing the importRelations() method:
Then import without specifying relations:
Combining Multiple Relations
Relation Configuration Reference
| Option | Type | Description |
|---|---|---|
type |
string | Required. Relation type (belongsTo, hasMany, etc.) |
model |
string | Required. Related model class |
file_column |
string | Single file column to read from |
file_columns |
array | Multiple file columns (for composite keys) |
lookup_by |
string | Column to search by in related table |
lookup_columns |
array | Multiple columns for composite key lookup |
create_if_missing |
bool | Create related record if not found (default: false) |
create_mapping |
array | Column mapping when creating related record |
mapping |
array | Column mapping for hasOne/hasMany child records |
defaults |
array | Default values when creating records |
foreign_key |
string | Explicit foreign key column name |
owner_key |
string | Parent model's key column (default: 'id') |
local_key |
string | Local model's key column (default: 'id') |
separator |
string | Separator for parsing multiple values (default: ',') |
sync_mode |
string | For belongsToMany: sync, attach, toggle (default: 'sync') |
pivot_data |
array | Static data for pivot table |
pivot_columns |
array | Map file columns to pivot columns |
morph_name |
string | Morph name for polymorphic relations |
morph_type_column |
string | Morph type column name |
morph_id_column |
string | Morph ID column name |
through_model |
string | Intermediate model for through relations |
first_key |
string | First key for through relations |
second_key |
string | Second key for through relations |
transformers |
array | Field transformers for this relation |
Implementing Importable Interface
For models with validation rules, implement the Importable interface:
Import Model
The package creates import records to track progress:
Cancelling Imports
Events
The package fires several events during the import lifecycle:
Event Data
Exception Handling
Example Controller
Testing
Security
The package includes security measures:
- Model Whitelist: Only models listed in
allowed_modelsconfig can be imported - File Validation: Only supported file types are processed
- Input Validation: All rows are validated before import
Performance Tips
- Use queue processing for large files
- Adjust
chunk_sizebased on your server's memory - Disable model events with
withoutModelEvents()for faster imports - Use
batchInsert(default) instead of individual inserts - For imports with
hasMany/belongsToManyrelations, records are inserted individually (not batched) to support relation processing
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-smart-importer with dependencies
illuminate/support Version ^10.0|^11.0
illuminate/database Version ^10.0|^11.0
illuminate/queue Version ^10.0|^11.0
illuminate/validation Version ^10.0|^11.0
illuminate/events Version ^10.0|^11.0
maatwebsite/excel Version ^3.1
league/csv Version ^9.0