| Install | |
|---|---|
composer require sazzadbinashique/laravel-excel-importer |
|
| Latest Version: | v2.0.3 |
| PHP: | ^8.2 |
Import any Laravel model from Excel/CSV with preview, progress tracking, validation errors, and a built-in dashboard.
composer require sazzadbinashique/laravel-excel-importer
Publish package assets:
php artisan vendor:publish --provider="SazzadBinAshique\LaravelExcelImporter\ExcelImporterServiceProvider"
Run migrations:
php artisan migrate
The package registers a ready-to-use dashboard route (protected by auth by default):
/excel-importer (name: excel-importer.dashboard)/excel-importer/{type} (name: excel-importer.dashboard.type)Add a button anywhere in your app:
<a href="{{ route('excel-importer.dashboard') }}">Open Import Dashboard</a>
Define your import types in the published config:
// config/excel-importer.php
'import_types' => [
'users' => \App\Imports\UsersImport::class,
'products' => \App\Imports\ProductsImport::class,
],
Extend the base import to get validation, batching, and progress updates:
<?php
namespace App\Imports;
use App\Models\User;
use SazzadBinAshique\LaravelExcelImporter\Imports\BaseImport;
use SazzadBinAshique\LaravelExcelImporter\Models\Import;
use Illuminate\Support\Facades\Hash;
class UsersImport extends BaseImport
{
public function __construct(Import $import)
{
parent::__construct($import);
}
public function model(array $row)
{
return new User([
'name' => $row['name'],
'email' => $row['email'],
'password' => Hash::make($row['password'] ?? 'password'),
]);
}
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email', 'unique:users,email'],
];
}
}
The import job runs on the queue. Set your queue connection and run a worker:
QUEUE_CONNECTION=database
php artisan queue:work
Old imports and error files can be cleaned up automatically:
php artisan excel-importer:cleanup --days=7
Schedule it (optional):
// app/Console/Kernel.php
$schedule->command('excel-importer:cleanup')->dailyAt(config('excel-importer.cleanup_time'));
Key settings in config/excel-importer.php:
storage_disk (default local)import_path and error_pathbatch_size, chunk_size, preview_rowsroute_prefix and middlewarerecent_importsFrom the package root:
composer install
vendor/bin/phpunit
Manual test inside a Laravel app:
config/excel-importer.php.php artisan migrate.php artisan queue:work.route('excel-importer.dashboard') and import a sample CSV.If the dashboard route returns 404, ensure the package service provider is discovered.
If progress never updates, ensure the queue worker is running.
If files are missing, check the configured storage_disk and import_path.
{
return config('excel-importer.batch_size', 100);
}
public function chunkSize(): int { return config('excel-importer.chunk_size', 100); }
public function registerEvents(): array { return [ AfterBatch::class => function(AfterBatch $event) { $batchSize = $event->getConcernable()->batchSize(); $currentFailures = $this->failures()->count();
$this->processedRows += $batchSize;
$this->successfulRows = $this->processedRows - $currentFailures;
// Update import progress
$this->import->update([
'processed_rows' => $this->processedRows,
'successful_rows' => $this->successfulRows,
'failed_rows' => $currentFailures,
]);
},
];
} }
## Configuration
Edit `config/excel-importer.php`:
```php
return [
// Storage disk for files
'storage_disk' => 'local',
// File retention period (days)
'retention_days' => 1,
// Daily cleanup time (24-hour format)
'cleanup_time' => '12:00',
// Batch and chunk sizes
'batch_size' => 100,
'chunk_size' => 100,
// Max file upload size (KB)
'max_file_size' => 10240, // 10MB
// Progress update interval (seconds)
'progress_interval' => 2,
// Queue settings
'queue_connection' => null,
'queue_name' => 'default',
];
Manually clean up import and error files:
# Clean files older than 1 day (default)
php artisan imports:cleanup
# Clean files older than 7 days
php artisan imports:cleanup --days=7
# Clean files older than 30 days
php artisan imports:cleanup --days=30
This command removes:
storage/app/importsstorage/app/errorsThe package automatically schedules cleanup daily at 12:00 PM (configurable). The scheduler runs:
php artisan imports:cleanup --days=1
Change cleanup time in config:
'cleanup_time' => '02:00', // Run at 2:00 AM
Or via environment variable:
EXCEL_IMPORTER_CLEANUP_TIME=02:00
| name | password | |
|---|---|---|
| John Doe | john@example.com | secret123 |
| Jane Smith | jane@example.com | pass1234 |
The package includes sample file generators:
# Generate 10,000 row sample file
php generate-sample.php
This creates public/sample-10k.csv with unique, valid test data.
Publish and modify the Livewire component:
php artisan vendor:publish --tag=excel-importer-views
Edit: resources/views/vendor/excel-importer/livewire/excel-importer.blade.php
Update your import class's rules() method:
public function rules(): array
{
return [
'email' => ['required', 'email', 'unique:users,email'],
'name' => ['required', 'string', 'max:255'],
'phone' => ['nullable', 'regex:/^[0-9]{10}$/'],
'age' => ['nullable', 'integer', 'min:18', 'max:100'],
];
}
Override customValidationMessages():
public function customValidationMessages()
{
return [
'email.unique' => 'This email is already registered.',
'phone.regex' => 'Phone number must be 10 digits.',
'age.min' => 'User must be at least 18 years old.',
];
}
Adjust performance by changing batch and chunk sizes:
public function batchSize(): int
{
return 500; // Process 500 rows at a time
}
public function chunkSize(): int
{
return 500; // Read 500 rows at a time
}
Or in config:
EXCEL_IMPORTER_BATCH_SIZE=500
EXCEL_IMPORTER_CHUNK_SIZE=500
storage/app/importsstorage/app/errorsCheck queue is running:
php artisan queue:work
Check queue connection in .env:
QUEUE_CONNECTION=database
Increase PHP limits in php.ini:
upload_max_filesize = 20M
post_max_size = 20M
max_execution_time = 300
Check storage permissions:
chmod -R 775 storage
chmod -R 775 bootstrap/cache
Clear config cache:
php artisan config:clear
php artisan cache:clear
Restart queue worker:
php artisan queue:restart
php artisan queue:work
Verify crontab entry:
crontab -l
Test scheduler manually:
php artisan schedule:run
Check scheduled tasks:
php artisan schedule:list
Create test files to verify functionality:
Valid data (sample-import.csv):
name,email,password
John Doe,john@example.com,password123
Jane Smith,jane@example.com,secret456
Invalid data (sample-with-errors.csv):
name,email,password
,missing@name.com,pass
John,,short
Duplicate,john@example.com,test
--queue=default --sleep=3 --tries=3This package is open-sourced software licensed under the MIT license.
For issues, questions, or contributions, please visit the GitHub repository.