Skip to content

Commit dcfdec3

Browse files
authored
Update readme.md
Improve code highlighting
1 parent 099ca5d commit dcfdec3

File tree

1 file changed

+89
-72
lines changed

1 file changed

+89
-72
lines changed

Diff for: readme.md

+89-72
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,51 @@ Require this package in your composer.json and run composer update (or run `comp
1818

1919
**For PHP 7.4+**
2020

21-
"flynsarmy/csv-seeder": "2.0.*"
21+
```json
22+
"flynsarmy/csv-seeder": "2.0.*"
23+
```
2224

2325
**For older PHP versions**
2426

25-
"flynsarmy/csv-seeder": "1.*"
26-
27+
```json
28+
"flynsarmy/csv-seeder": "1.*"
29+
```
2730

2831
### Usage
2932

3033
Your CSV's header row should match the DB columns you wish to import. IE to import *id* and *name* columns, your CSV should look like:
3134

32-
id,name
33-
1,Foo
34-
2,Bar
35+
```csv
36+
id,name
37+
1,Foo
38+
2,Bar
39+
```
3540

3641
Seed classes must extend `Flynsarmy\CsvSeeder\CsvSeeder`, they must define the destination database table and CSV file path, and finally they must call `parent::run()` like so:
3742

38-
use Flynsarmy\CsvSeeder\CsvSeeder;
43+
```php
44+
use Flynsarmy\CsvSeeder\CsvSeeder;
3945

40-
class StopsTableSeeder extends CsvSeeder {
46+
class StopsTableSeeder extends CsvSeeder {
4147

42-
public function __construct()
43-
{
44-
$this->table = 'your_table';
45-
$this->filename = base_path().'/database/seeds/csvs/your_csv.csv';
46-
}
48+
public function __construct()
49+
{
50+
$this->table = 'your_table';
51+
$this->filename = base_path().'/database/seeds/csvs/your_csv.csv';
52+
}
4753

48-
public function run()
49-
{
50-
// Recommended when importing larger CSVs
51-
DB::disableQueryLog();
54+
public function run()
55+
{
56+
// Recommended when importing larger CSVs
57+
DB::disableQueryLog();
5258

53-
// Uncomment the below to wipe the table clean before populating
54-
DB::table($this->table)->truncate();
59+
// Uncomment the below to wipe the table clean before populating
60+
DB::table($this->table)->truncate();
5561

56-
parent::run();
57-
}
62+
parent::run();
5863
}
64+
}
65+
```
5966

6067
Drop your CSV into */database/seeds/csvs/your_csv.csv* or whatever path you specify in your constructor above.
6168

@@ -78,66 +85,76 @@ In addition to setting the database table and CSV filename, the following config
7885
### Examples
7986
CSV with pipe delimited values:
8087

81-
public function __construct()
82-
{
83-
$this->table = 'users';
84-
$this->csv_delimiter = '|';
85-
$this->filename = base_path().'/database/seeds/csvs/your_csv.csv';
86-
}
87-
88+
```php
89+
public function __construct()
90+
{
91+
$this->table = 'users';
92+
$this->csv_delimiter = '|';
93+
$this->filename = base_path().'/database/seeds/csvs/your_csv.csv';
94+
}
95+
```
96+
8897
Specifying which CSV columns to import:
8998

90-
public function __construct()
91-
{
92-
$this->table = 'users';
93-
$this->csv_delimiter = '|';
94-
$this->filename = base_path().'/database/seeds/csvs/your_csv.csv';
95-
$this->mapping = [
96-
0 => 'first_name',
97-
1 => 'last_name',
98-
5 => 'age',
99-
];
100-
}
101-
99+
```php
100+
public function __construct()
101+
{
102+
$this->table = 'users';
103+
$this->csv_delimiter = '|';
104+
$this->filename = base_path().'/database/seeds/csvs/your_csv.csv';
105+
$this->mapping = [
106+
0 => 'first_name',
107+
1 => 'last_name',
108+
5 => 'age',
109+
];
110+
}
111+
```
112+
102113
Trimming the whitespace from the imported data:
103114

104-
public function __construct()
105-
{
106-
$this->table = 'users';
107-
$this->csv_delimiter = '|';
108-
$this->filename = base_path().'/database/seeds/csvs/your_csv.csv';
109-
$this->mapping = [
110-
0 => 'first_name',
111-
1 => 'last_name',
112-
5 => 'age',
113-
];
114-
$this->should_trim = true;
115-
}
116-
115+
```php
116+
public function __construct()
117+
{
118+
$this->table = 'users';
119+
$this->csv_delimiter = '|';
120+
$this->filename = base_path().'/database/seeds/csvs/your_csv.csv';
121+
$this->mapping = [
122+
0 => 'first_name',
123+
1 => 'last_name',
124+
5 => 'age',
125+
];
126+
$this->should_trim = true;
127+
}
128+
```
129+
117130
Skipping the CSV header row (Note: A mapping is required if this is done):
118131

119-
public function __construct()
120-
{
121-
$this->table = 'users';
122-
$this->csv_delimiter = '|';
123-
$this->filename = base_path().'/database/seeds/csvs/your_csv.csv';
124-
$this->offset_rows = 1;
125-
$this->mapping = [
126-
0 => 'first_name',
127-
1 => 'last_name',
128-
2 => 'password',
129-
];
130-
$this->should_trim = true;
131-
}
132+
```php
133+
public function __construct()
134+
{
135+
$this->table = 'users';
136+
$this->csv_delimiter = '|';
137+
$this->filename = base_path().'/database/seeds/csvs/your_csv.csv';
138+
$this->offset_rows = 1;
139+
$this->mapping = [
140+
0 => 'first_name',
141+
1 => 'last_name',
142+
2 => 'password',
143+
];
144+
$this->should_trim = true;
145+
}
146+
```
132147

133148
Specifying the DB connection to use:
134149

135-
public function __construct()
136-
{
137-
$this->table = 'users';
138-
$this->connection = 'my_connection';
139-
$this->filename = base_path().'/database/seeds/csvs/your_csv.csv';
140-
}
150+
```php
151+
public function __construct()
152+
{
153+
$this->table = 'users';
154+
$this->connection = 'my_connection';
155+
$this->filename = base_path().'/database/seeds/csvs/your_csv.csv';
156+
}
157+
```
141158

142159
### Migration Guide
143160

0 commit comments

Comments
 (0)