Skip to content

Commit 36f7622

Browse files
Added Readme for the RemembersRowNumberTrait (SpartnerNL#99)
* Added Readme for the RemembersRowNumberTrait * Update chunk-reading.md Co-authored-by: Patrick Brouwers <[email protected]>
1 parent c975c51 commit 36f7622

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

3.1/imports/chunk-reading.md

+37-3
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,48 @@ class UsersImport implements ToModel, WithBatchInserts, WithChunkReading
6868

6969
## Keep Track of the Row number
7070

71-
If you need the information about the offset of the chunk you can use the `RemembersChunkOffset` Trait.
71+
If you need the row number you can use the `RemembersRowNumber` Trait.
7272

7373
```php
7474
namespace App\Imports;
7575

7676
use App\User;
7777
use Maatwebsite\Excel\Concerns\ToModel;
78-
use Maatwebsite\Excel\Concerns\WithChunkOffset;
78+
use Maatwebsite\Excel\Concerns\RemembersRowNumber;
79+
use Maatwebsite\Excel\Concerns\WithChunkReading;
80+
81+
class UsersImport implements ToModel, WithChunkReading
82+
{
83+
use RemembersRowNumber;
84+
85+
public function model(array $row)
86+
{
87+
$currentRowNumber = $this->getRowNumber();
88+
89+
return new User([
90+
'name' => $row[0],
91+
]);
92+
}
93+
94+
public function chunkSize(): int
95+
{
96+
return 1000;
97+
}
98+
}
99+
```
100+
101+
::: warning
102+
Remembering row numbers is only intender for ToModel imports.
103+
:::
104+
105+
If you only need the information about the offset of the chunk you can use the `RemembersChunkOffset` Trait.
106+
107+
```php
108+
namespace App\Imports;
109+
110+
use App\User;
111+
use Maatwebsite\Excel\Concerns\ToModel;
112+
use Maatwebsite\Excel\Concerns\RemembersChunkOffset;
79113
use Maatwebsite\Excel\Concerns\WithChunkReading;
80114

81115
class UsersImport implements ToModel, WithChunkReading
@@ -96,4 +130,4 @@ class UsersImport implements ToModel, WithChunkReading
96130
return 1000;
97131
}
98132
}
99-
```
133+
```

0 commit comments

Comments
 (0)