@@ -18,44 +18,51 @@ Require this package in your composer.json and run composer update (or run `comp
18
18
19
19
** For PHP 7.4+**
20
20
21
- "flynsarmy/csv-seeder": "2.0.*"
21
+ ``` json
22
+ "flynsarmy/csv-seeder" : " 2.0.*"
23
+ ```
22
24
23
25
** For older PHP versions**
24
26
25
- "flynsarmy/csv-seeder": "1.*"
26
-
27
+ ``` json
28
+ "flynsarmy/csv-seeder" : " 1.*"
29
+ ```
27
30
28
31
### Usage
29
32
30
33
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:
31
34
32
- id,name
33
- 1,Foo
34
- 2,Bar
35
+ ``` csv
36
+ id,name
37
+ 1,Foo
38
+ 2,Bar
39
+ ```
35
40
36
41
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:
37
42
38
- use Flynsarmy\CsvSeeder\CsvSeeder;
43
+ ``` php
44
+ use Flynsarmy\CsvSeeder\CsvSeeder;
39
45
40
- class StopsTableSeeder extends CsvSeeder {
46
+ class StopsTableSeeder extends CsvSeeder {
41
47
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
+ }
47
53
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();
52
58
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();
55
61
56
- parent::run();
57
- }
62
+ parent::run();
58
63
}
64
+ }
65
+ ```
59
66
60
67
Drop your CSV into * /database/seeds/csvs/your_csv.csv* or whatever path you specify in your constructor above.
61
68
@@ -78,66 +85,76 @@ In addition to setting the database table and CSV filename, the following config
78
85
### Examples
79
86
CSV with pipe delimited values:
80
87
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
+
88
97
Specifying which CSV columns to import:
89
98
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
+
102
113
Trimming the whitespace from the imported data:
103
114
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
+
117
130
Skipping the CSV header row (Note: A mapping is required if this is done):
118
131
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
+ ```
132
147
133
148
Specifying the DB connection to use:
134
149
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
+ ```
141
158
142
159
### Migration Guide
143
160
0 commit comments