Laravel Seeder for Different Environments

laravel, laravel-4, seed

Solution

I just tried this quickly and got it working, so I'll show you how I set it up and hopefully that helps.

app/database/seeds/local/UsersTableSeeder.php

<?php namespace Seeds\Local;

use Illuminate\Database\Seeder as Seeder;

Class UsersTableSeeder extends Seeder {
    public function run () {
        dd('local');
    }
}

app/database/seeds/production/UsersTableSeeder.php

<?php namespace Seeds\Production;

use Illuminate\Database\Seeder as Seeder;

Class UsersTableSeeder extends Seeder {
    public function run () {
        dd('production');
    }
}

app/database/seeds/DatabaseSeeder.php

<?php

class DatabaseSeeder extends Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run(){
        Eloquent::unguard();

        // Load production seeder
        if (App::Environment() === 'production')
        {
            $this->call('Seeds\Production\UsersTableSeeder');
        }

        // Load local seeder
        if (App::Environment() === 'local')
        {
            $this->call('Seeds\Local\UsersTableSeeder');
        }
    }

}

And don't forget to run composer dump-autoload. Hope that helps.

Problem

I created two folders in my seeder folder: ``` /seeds /local /production DatabaseSeeder.php ``` Then, defined the following inside `DatabaseSeeder.php` class DatabaseSeeder extends Seeder { ``` /** * Run the database seeds. * * @return void */ public function run() { Eloquent::unguard(); // Load production seeder if (App::Environment() === 'production') { $this->call('production/UsersTableSeeder'); } // Load local seeder if (App::Environment() === 'local') { $this->call('local/UsersTableSeeder'); } } ``` } Now I know I can't do `call('local/UsersTablderSeeder')`, and that is my question. How can I `call()` the seeder files from their respective folders? Edit To be clear, when I run the code as it is shown above, I get the following error ``` [ReflectionException] Class local/UsersTableSeeder does not exist ```

Original source