Skip to content

Commit cdf4803

Browse files
committed
Merge pull request #15 from basementjack/master
skip CSV columns that don't exist in the database
2 parents 6ee1358 + e6e65ee commit cdf4803

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/Flynsarmy/CsvSeeder/CsvSeeder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use DB;
66
use Hash;
77
use Illuminate\Database\Seeder;
8+
use Illuminate\Database\Schema;
89

910
/**
1011
* Taken from http://laravelsnippets.com/snippets/seeding-database-with-csv-files-cleanly
@@ -160,6 +161,13 @@ public function seedFromCSV($filename, $deliminator = ",")
160161
{
161162
$mapping = $row;
162163
$mapping[0] = $this->stripUtf8Bom($mapping[0]);
164+
165+
// skip csv columns that don't exist in the database
166+
foreach($mapping as $index => $fieldname){
167+
if (!DB::getSchemaBuilder()->hasColumn($this->table, $fieldname)){
168+
array_pull($mapping, $index);
169+
}
170+
}
163171
}
164172
else
165173
{

0 commit comments

Comments
 (0)