Skip to content

Commit e6e65ee

Browse files
committed
skip CSV columns that don't exist in the database
If no mapping is found, first row is used, but csv columns not found in the DB are stripped out to prevent insert errors. This is done only once, up at the top, and is only done if no mapping is specified.
1 parent 6ee1358 commit e6e65ee

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)