Skip to content

Commit 347675c

Browse files
committed
Implement WithMapping in the importer
This is important, because `map()` will be called _before_ validation. The import will always fail otherwise, because the unmapped columns are passed through validation. This way we map column names before validating, and we don't have to map in the `model()` method.
1 parent 959f1d7 commit 347675c

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/Importer.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
use Maatwebsite\Excel\Concerns\WithValidation;
1414
use Maatwebsite\Excel\Concerns\WithBatchInserts;
1515
use Maatwebsite\Excel\Concerns\WithChunkReading;
16+
use Maatwebsite\Excel\Concerns\WithMapping;
1617

17-
class Importer implements ToModel, WithValidation, WithHeadingRow, WithBatchInserts, WithChunkReading, SkipsOnFailure, SkipsOnError
18+
class Importer implements ToModel, WithValidation, WithHeadingRow, WithMapping, WithBatchInserts, WithChunkReading, SkipsOnFailure, SkipsOnError
1819
{
1920
use Importable, SkipsFailures, SkipsErrors;
2021

@@ -25,12 +26,14 @@ class Importer implements ToModel, WithValidation, WithHeadingRow, WithBatchInse
2526
protected $rules;
2627
protected $model_class;
2728

29+
public function map($row): array {
30+
return $this->mapRowDataToAttributes($row);
31+
}
32+
2833
public function model(array $row)
2934
{
30-
[$model, $callbacks] = $this->resource::fill(
31-
new ImportRequest($this->mapRowDataToAttributes($row)),
32-
$this->resource::newModel()
33-
);
35+
$model = $this->resource::newModel();
36+
$model->fill($row);
3437

3538
return $model;
3639
}

0 commit comments

Comments
 (0)