Skip to content

Commit b723f68

Browse files
committed
adding csv importer job
1 parent dabb3cd commit b723f68

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed
Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
namespace Coderflex\LaravelCsv\Http\Livewire;
44

55
use Coderflex\LaravelCsv\Concerns;
6+
use Coderflex\LaravelCsv\Jobs\ImportCsv;
67
use Coderflex\LaravelCsv\Utilities\ChunkIterator;
8+
use Illuminate\Support\Facades\Bus;
79
use Illuminate\Support\MessageBag;
810
use Illuminate\Validation\Validator;
911
use Livewire\Component;
1012
use Livewire\WithFileUploads;
1113

12-
class ImportCsv extends Component
14+
class CsvImporter extends Component
1315
{
1416
use WithFileUploads;
1517
use Concerns\InteractsWithColumns;
@@ -36,6 +38,12 @@ class ImportCsv extends Component
3638
/** @var int */
3739
public int $fileRowCount = 0;
3840

41+
/** @var array */
42+
protected $exception = [
43+
'mode', 'columnsToMap', 'open',
44+
'columnLabels', 'requiredColumns',
45+
];
46+
3947
public function mount()
4048
{
4149
// map and coverts the columnsToMap property into an associative array
@@ -61,9 +69,11 @@ public function import()
6169
{
6270
$this->validate();
6371

64-
$import = $this->createNewImport();
72+
$this->importCsv();
6573

66-
$chunks = (new ChunkIterator($this->csvRecords->getIterator(), 10))->get();
74+
$this->resetExcept($this->exceptions);
75+
76+
$this->emitTo('csv-imports', 'imports.refresh');
6777
}
6878

6979
public function render()
@@ -107,4 +117,25 @@ protected function createNewImport()
107117
'total_rows' => $this->fileRowCount,
108118
]);
109119
}
120+
121+
protected function importCsv()
122+
{
123+
$import = $this->createNewImport();
124+
$chunks = (new ChunkIterator($this->csvRecords->getIterator(), 10))->get();
125+
126+
$jobs = collect($chunks)
127+
->map(
128+
fn ($chunk) => new ImportCsv(
129+
$import,
130+
$this->model,
131+
$chunk,
132+
$this->columnsToMap
133+
)
134+
);
135+
136+
Bus::batch($jobs)
137+
->finally(
138+
fn () => $import->touch('compoleted_at')
139+
)->dispatch();
140+
}
110141
}

src/Jobs/ImportCsv.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelCsv\Jobs;
4+
5+
use Coderflex\LaravelCsv\Models\Import;
6+
use Illuminate\Bus\Batchable;
7+
use Illuminate\Bus\Queueable;
8+
use Illuminate\Contracts\Queue\ShouldQueue;
9+
use Illuminate\Foundation\Bus\Dispatchable;
10+
use Illuminate\Queue\InteractsWithQueue;
11+
use Illuminate\Queue\SerializesModels;
12+
13+
class ImportCsv implements ShouldQueue
14+
{
15+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
16+
17+
use Batchable;
18+
19+
/**
20+
* Create a new job instance.
21+
*
22+
* @return void
23+
*/
24+
public function __construct(
25+
public Import $import,
26+
public string $model,
27+
public array $chunk,
28+
public array $columns,
29+
) { /** */}
30+
31+
/**
32+
* Execute the job.
33+
*
34+
* @return void
35+
*/
36+
public function handle()
37+
{
38+
$affectedRows = $this->model::upsert(
39+
$this->chunk,
40+
['id'],
41+
collect($this->columns)->diff('id')->keys()->toArray(),
42+
);
43+
44+
$this->import->increment('processed_rows', $affectedRows);
45+
}
46+
}

0 commit comments

Comments
 (0)