Skip to content

Commit 0b99e38

Browse files
committed
Add batching, chunking, skipping errors and failures
1 parent 67923c0 commit 0b99e38

File tree

2 files changed

+39
-28
lines changed

2 files changed

+39
-28
lines changed

src/Http/Controllers/ImportController.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,14 @@ public function import(NovaRequest $request, $file)
7272
->setAttributes($attributes)
7373
->setAttributeMap($attribute_map)
7474
->setRules($rules)
75-
->setModelClass($model_class);
76-
77-
try {
78-
$this->importer->import($this->getFilePath($file), null, 'Csv');
79-
} catch (QueryException $e) {
80-
throw new \Exception($e->getPrevious()->errorInfo[2]);
81-
} catch (ImportException $e) {
82-
$this->responseError($e->getMessage());
83-
} catch (NoTypeDetectedException $e) {
84-
$this->responseError(__('Invalid file type'));
75+
->setModelClass($model_class)
76+
->import($this->getFilePath($file), null, 'Csv');
77+
78+
if (! $this->importer->failures()->isEmpty() || ! $this->importer->errors()->isEmpty()) {
79+
return response()->json(['result' => 'failure', 'errors' => $this->importer->errors(), 'failures' => $this->importer->failures()]);
8580
}
8681

87-
return response()->json(['result' => 'success', ]);
82+
return response()->json(['result' => 'success']);
8883
}
8984

9085
protected function extractValidationRules($request, NovaResource $resource)

src/Importer.php

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@
55
use Laravel\Nova\Resource;
66
use Maatwebsite\Excel\Concerns\ToModel;
77
use Maatwebsite\Excel\Concerns\Importable;
8+
use Maatwebsite\Excel\Concerns\SkipsErrors;
9+
use Maatwebsite\Excel\Concerns\SkipsOnError;
10+
use Maatwebsite\Excel\Concerns\SkipsFailures;
11+
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
812
use Maatwebsite\Excel\Concerns\WithHeadingRow;
913
use Maatwebsite\Excel\Concerns\WithValidation;
14+
use Maatwebsite\Excel\Concerns\WithBatchInserts;
15+
use Maatwebsite\Excel\Concerns\WithChunkReading;
1016

11-
class Importer implements ToModel, WithValidation, WithHeadingRow
17+
class Importer implements ToModel, WithValidation, WithHeadingRow, WithBatchInserts, WithChunkReading, SkipsOnFailure, SkipsOnError
1218
{
13-
use Importable;
19+
use Importable, SkipsFailures, SkipsErrors;
1420

1521
/** @var Resource */
1622
protected $resource;
@@ -19,6 +25,31 @@ class Importer implements ToModel, WithValidation, WithHeadingRow
1925
protected $rules;
2026
protected $model_class;
2127

28+
public function model(array $row)
29+
{
30+
[$model, $callbacks] = $this->resource::fill(
31+
new ImportRequest($this->mapRowDataToAttributes($row)),
32+
$this->resource::newModel()
33+
);
34+
35+
return $model;
36+
}
37+
38+
public function rules(): array
39+
{
40+
return $this->rules;
41+
}
42+
43+
public function batchSize(): int
44+
{
45+
return 100;
46+
}
47+
48+
public function chunkSize(): int
49+
{
50+
return 100;
51+
}
52+
2253
/**
2354
* @return mixed
2455
*/
@@ -57,11 +88,6 @@ public function setAttributeMap($map)
5788
return $this;
5889
}
5990

60-
public function rules(): array
61-
{
62-
return $this->rules;
63-
}
64-
6591
/**
6692
* @param mixed $rules
6793
* @return Importer
@@ -92,16 +118,6 @@ public function setModelClass($model_class)
92118
return $this;
93119
}
94120

95-
public function model(array $row)
96-
{
97-
[$model, $callbacks] = $this->resource::fill(
98-
new ImportRequest($this->mapRowDataToAttributes($row)),
99-
$this->resource::newModel()
100-
);
101-
102-
return $model;
103-
}
104-
105121
public function setResource($resource)
106122
{
107123
$this->resource = $resource;

0 commit comments

Comments
 (0)