Skip to content

Commit 942af6a

Browse files
committed
docs: with headers
1 parent eeab95c commit 942af6a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,34 @@ You can set the table to load the data into using the `into` method. This method
138138
$loader->into('employees', ['name', 'dept_id']);
139139
```
140140
141+
### With Headers
142+
143+
Using `withHeaders` will skip the first row of the CSV file.
144+
145+
> **IMPORTANT**
146+
> This method assumes that the headers are the same as the columns.
147+
> If the headers are different from the columns, you should define the `columns` in the `into` method.
148+
> This works well with the Eloquent Collection as the data source and saving it to a CSV file for bulk import.
149+
150+
#### Building a CSV File from Eloquent Collection
151+
152+
```php
153+
$users = User::all();
154+
Yajra\SQLLoader\CsvFile::make(database_path('files/users.csv'), 'w')
155+
->headers(array_keys($users->first()->toArray()))
156+
->insert($users->toArray())
157+
->close();
158+
```
159+
160+
#### Loading CSV File with Headers
161+
162+
```php
163+
$loader->inFile(database_path('files/users.csv'))
164+
->withHeaders()
165+
->into('users')
166+
->execute();
167+
```
168+
141169
### Disk
142170
143171
You can set the disk to use for the control file using the `disk` method.

0 commit comments

Comments
 (0)