eComEvo
eComEvo

Reputation: 12551

Tables are not seeding in Laravel

I am trying to get my tables seeded with initial data to begin development work in Laravel 4.1 but none of them are seeding.

For instance, when I run php artisan db:seed on the following I am given the output Seeded: ItemsTableSeeder:

use Faker\Factory as Faker;

class ItemsTableSeeder extends Seeder {

    public function run()
    {
        $f = Faker::create();

        foreach(range(1, 10) as $index)
        {
            $price = round(rand(5, 100) + (rand(0, 99) / 10), 2);
            $name = $f->sentence(6);
            $shortname = explode(' ', $name, 3);

            Item::create([
                'sku'       => strtoupper(str_random(16)),
                'name'      => $name,
                'shortname' => implode(' ', array_slice($shortname, 0, 3)),
                'stock'     => rand(0, 100),
                'price'     => $price,
                'cost'      => round($price / rand(2, 3), 2),
                'weight'    => rand(0, 20) + (rand(1, 9) / 10),
                'width'     => rand(1, 20),
                'height'    => rand(1, 20),
                'length'    => rand(1, 20),
                'size'      => array_rand(['Standard', 'Oversized'], 1),
                'enabled'   => 'Y'
            ]);
        }
    }

}

However, a quick SELECT in MySQL Workbench shows that table is still empty. I have this setup to seed before any other tables because tables after items must do a lookup to reference item ids from that table to create their own seeds. When they can't find any item ids, it breaks the rest of the seeds after this.

Upvotes: 2

Views: 2233

Answers (2)

Worp
Worp

Reputation: 1018

For future users coming here:

You need to add every seeder class you're creating to DatabaseSeeder.php like so:

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        // Add every single seeder class here
        $this->call(UserSeeder::class);
        $this->call(NewsSeeder::class);
    }
}

Only then they are called when running php artisan db:seed.

Upvotes: 5

eComEvo
eComEvo

Reputation: 12551

The detectEnvironment() settings in bootstrap/start.php weren't overriding correctly so it was defaulting to sqlite in the default app/config/database.php file. Seeding was happening in the default sqlite database file.

I had used dev instead of local as the key to the development server.

Upvotes: 2

Related Questions