Installation:
composer require coderflex/laravel-csv
Publish the config (if needed):
php artisan vendor:publish --provider="Coderflex\LaravelCsv\LaravelCsvServiceProvider" --tag="config"
First Use Case:
@livewire('csv-importer', ['model' => \App\Models\YourModel::class])
<x-csv-import-button :model="\App\Models\YourModel" />
Key Files to Review:
config/laravel-csv.php (for chunk size, queue settings, etc.)app/Http/Livewire/CsvImporter.php (default component logic)resources/views/vendor/laravel-csv/ (blade templates)Livewire Integration:
namespace App\Http\Livewire;
use Coderflex\LaravelCsv\Livewire\CsvImporter as BaseImporter;
class CustomCsvImporter extends BaseImporter {
protected $rules = [
'file' => 'required|mimes:csv,txt',
];
public function import() {
$this->validate();
$this->processFile($this->file->getRealPath(), \App\Models\YourModel::class);
}
}
processFile() for default handling or override handleRow() for custom row processing.Queue-Based Processing:
config/laravel-csv.php:
'chunk_size' => 1000, // Adjust based on server memory
use Coderflex\LaravelCsv\Jobs\ProcessCsvChunk;
ProcessCsvChunk::dispatch($filePath, \App\Models\YourModel::class, $chunk);
TALL Stack Integration:
<x-csv-import-form :model="\App\Models\YourModel" />
php artisan vendor:publish --tag="laravel-csv-views"
Validation & Mapping:
protected $rules = [
'file' => 'required|mimes:csv',
'column_mapping' => 'sometimes|array',
];
public function getColumnMapping() {
return [
'csv_column_name' => 'model_attribute',
];
}
Memory Issues:
chunk_size = 500 and monitor server logs. Use queues ('use_queue' => true) for files >100K rows.Column Mismatches:
getColumnMapping() or use auto_map: true in config:
'auto_map' => true, // Auto-detects column names (e.g., "first_name" → "firstName")
Livewire State Persistence:
session(['csv_temp_path' => $file->getRealPath()]);
Queue Stuck Jobs:
failed_jobs table and ensure:
php artisan queue:work)..env.handleRow():
\Log::debug('Processing row:', ['data' => $row, 'model' => $model]);
League\Csv\Reader to inspect the file before processing:
$csv = Reader::createFromPath($filePath, 'r');
$csv->setHeaderOffset(0);
$records = $csv->getRecords();
Custom Importers:
trait HandlesCustomCsv {
protected function validateRow(array $row) {
// Custom validation
}
}
Post-Import Actions:
imported() event in your Livewire component:
protected $listeners = ['imported' => 'notifyUser'];
public function notifyUser() {
toast()->success('Import completed!');
}
Batch Processing:
processInBatches() for large datasets:
$this->processInBatches($filePath, \App\Models\YourModel::class, 5000);
Progress Tracking:
public $progress = 0;
protected function incrementProgress() {
$this->progress += ($this->totalRows / 100);
$this->emit('progress', $this->progress);
}
How can I help you explore Laravel packages today?