1

I'm using this package: https://github.com/Flynsarmy/laravel-csv-seeder

Flynsarmy Csv seeder works fine if I manually create a file and save it as a csv, then seed my database. Artisan responds with

"Seeding: ProductsTableSeeder
Database seeding completed successfully."

However, if I save an Excel file as csv, I get the response,

"Seeding: ProductsTableSeeder"

It does not fail. No exceptions are thrown. But there is also no success message.

When I check the DB, the tables are empty.

Here's my migration 'up' method

 {
    Schema::create('products', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('sku')->nullable();
        $table->string('name');
        $table->string('species')->nullable();
        $table->string('fabric')->nullable();
        $table->string('panel')->nullable();
        $table->string('gauge_gasket')->nullable();
        $table->string('type')->nullable();
        $table->string('brand')->nullable();
        $table->string('brand_label')->nullable();
        $table->boolean('is_cjb');
        $table->boolean('is_osp');
        $table->boolean('is_starmark');
        $table->boolean('is_vision');
        $table->boolean('gasketed');;
        $table->string('color');
        $table->float('inside_width')->nullable();
        $table->float('inside_length')->nullable();
        $table->float('outside_width')->nullable();
        $table->float('outside_length')->nullable();
        $table->timestamps();
    });

Here's my implementation of Flynsarmy Csv Seeder:

use Flynsarmy\CsvSeeder\CsvSeeder;

    class ProductsTableSeeder extends CsvSeeder
    {
        /**
         * Run the database seeds.
         *
         * @return void
         */
              public function __construct()
          {
            $this->table = 'products';
            $this->filename = base_path().'/database/seeds/csvs/product_info.csv';
          }

          public function run()
          {
            // Recommended when importing larger CSVs
            DB::disableQueryLog();

            // Uncomment the below to wipe the table clean before populating
            DB::table($this->table)->truncate();

            parent::run();


          }
    }

Now, I suspected my issue is the csv file. I used csvlint.io and it says

*

Structural problem: Assumed header As there is no machine readable way to tell if your CSV has a header row, we have assumed that your CSV has one.

*

However, it gives this same message for the csv I manually created and that seems to work just fine.

I thought it might be a permission issue and so I did an ls -lr on my /database/seeds/csvs directory and I get

-rw-rw-r-- 1 secretname secretname 18315 Dec 19 14:07 product_info.csv

I even did what friends don't let friends do, (chmod 777), but that fixed nothing, so it isn't a permissions issue.

I've been repeatedly doing:

php artisan cache:clear
composer dumpautoload
php artisan migrate:fresh --seed

All I want to do is export from Excel to Csv and seed the database. Why is this such a problem?

Here's a sample of my csv file. I have tried with both an id column and without (since laravel adds an id and autoincrements)

    sku,name,species,fabric,panel,finish,color,type,inside_width,inside_length,outside_width,outside_length,
800124,air tray casket,,,,white,,,30.5,,,,
555555,alex,metal,blue crepe,,paint,light blue,metal caskets,24.5,76.75,28.5,83,
578645,alex,metal,rosetan crepe,,paint,copper,metal caskets,24.5,76.75,28.5,83,
524785,alex,metal,oyster crepe,,paint,silver,metal caskets,24.5,76.75,28.5,83,
483959,alex,metal,oyster crepe,,paint,white,metal caskets,24.5,76.75,28.5,83,

The flynsarmy csv they give as a sample was the one that worked successfully, It's simply

id,name
1,Foo
2,Bar

2 Answers2

1

I feel so dumb. Or smart, depending on how I look at it. I was searching for complex answers, looking over a gigantic csv. So I paired it down to a few items at a time.It's the coma's. Excel placed comas where they did not need to be (at the end of each line). Removing the coma from the end of each line solves the problem. Interestingly, I did not get a "database seeded successfully" message. It only said "Seeding: ProductsTableSeeder", but it did populate the tables this time. Well, Hello World! How's that for a first post? ;)

But you know what? That linter validated my csv and said all was well. Well, it was wrong!

0

I ran into the same problem.

Excel only place the extra comma if there is formatting like table border on the column that has no data.

Open the csv with excel and save it again should remove the extra commas

anchan42
  • 55
  • 4