8000 Added withSkipDuplicates import concern (#230) · anthony-dee/laravel-excel-docs@47b974e · GitHub
[go: up one dir, main page]

10000
Skip to content

Commit 47b974e

Browse files
authored
Added withSkipDuplicates import concern (SpartnerNL#230)
1 parent 722d130 commit 47b974e

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

3.1/imports/batch-inserts.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,26 @@ In the example above, if a user already exists with the same email, the row will
7070
:::warning
7171
All databases except SQL Server require the `uniqueBy` columns to have a "primary" or "unique" index.
7272
:::
73+
74+
## Skipping duplicate rows
75+
76+
For skipping duplicate models, you can additionally implement the `WithSkipDuplicates` concern.
77+
78+
```php
79+
class UsersImport implements ToModel, WithBatchInserts, WithSkipDuplicates
80+
{
81+
public function model(array $row)
82+
{
83+
return new User([
84+
'name' => $row[0],
85+
]);
86+
}
87+
88+
public function batchSize(): int
89+
{
90+
return 1000;
91+
}
92+
}
93+
```
94+
95+
In the example above, if a user already exists with the same primary or unique key, the row will be ignored. Behind the scenes, this feature uses the Laravel `insertOrIgnore` method to insert records while ignoring duplicates, preventing any errors that would normally occur due to duplicate entries.

3.1/imports/model.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ class UsersImport implements ToModel, WithUpserts, WithUpsertColumns
6969

7070
In this example, if a user already exists, only "name" and "role" columns will be updated.
7171

72+
## Skipping duplicate rows
73+
74+
In case you want to skip duplicate models, you can implement the `WithSkipDuplicates` concern.
75+
76+
```php
77+
class UsersImport implements ToModel, WithSkipDuplicates
78+
{
79+
// No additional methods are required for this concern
80+
}
81+
```
82+
83+
In the example above, if a user already exists with the same primary or unique key, the row will be ignored. Behind the scenes, this feature uses the Laravel `insertOrIgnore` method to insert records while ignoring duplicates, preventing any errors that would normally occur due to duplicate entries.
7284

7385
## Skipping rows
7486

0 commit comments

Comments
 (0)
0