composer require askdkc/livewire-csv
php artisan livecsv-setup
HasCsvImports trait to your target model (e.g., User.php):
use Askdkc\LivewireCsv\Concerns\HasCsvImports;
class User extends Authenticatable {
use HasCsvImports;
}
Embed the csv-importer component in a Blade view with minimal props:
<livewire:csv-importer
:model="App\Models\User::class"
:columns-to-map="['id', 'name', 'email']"
:required-columns="['name', 'email']"
:column-labels="['name' => 'Full Name', 'email' => 'Email']"
/>
Define Model Requirements:
:columns-to-map.:required-columns).:column-labels for user-friendly field names.Upsert Logic:
id for upserting. Override with :upsert-columns (requires a unique/index constraint on those columns).$table->unique(['email', 'name']); // For :upsert-columns="['email', 'name']"
Queue-Based Processing:
php artisan queue:work
php artisan vendor:publish --tag="csv-views"
npm run dev
<head> and <footer>:
@csvStyles
@csvScripts
config/livewire-csv.php:
'set_delimiter' => ';', // For TSV files
Use Alpine.js-powered buttons for imports:
<x-csv-button class="bg-blue-500 text-white px-4 py-2 rounded">
Import CSV
</x-csv-button>
Queue Dependencies:
php artisan queue:batches-table and php artisan migrate will break async imports.livecsv-setup) or manually execute:
php artisan queue:table
php artisan queue:batches-table
php artisan migrate
Upsert Constraints:
:upsert-columns requires a unique/index constraint. Missing this causes silent failures.SQLSTATE[23000] errors (duplicate key violations).File Size Limits:
file_upload_size).Delimiter Mismatches:
,) may fail for TSV files (;).'set_delimiter' => ';' in config and ensure files match.failed_jobs table or logs for exceptions. Retry with:
php artisan queue:retry all
:validation-errors prop to display them:
<livewire:csv-importer ... :validation-errors="$errors" />
Custom Validation:
Override validateRow() in your model’s HasCsvImports trait:
public function validateRow(array $row): void {
$this->validate($row, [
'email' => 'required|email|unique:users,email,'.$this->id,
]);
}
Pre/Post-Import Hooks:
Use model events (creating, updating) or queue listeners to extend logic.
Custom Views: Publish and override default views:
php artisan vendor:publish --tag="livewire-csv-views" --force
chunk() in your model’s import() method.:upsert-columns are indexed to speed up lookups:
$table->index(['email', 'name']);
How can I help you explore Laravel packages today?