7

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
Kousha
  • 32,871
  • 51
  • 172
  • 296
  • I dont understand your question - what is wrong with what you have written? – Laurence Jul 16 '14 at 01:21
  • Please see above, the file is not found – Kousha Jul 16 '14 at 01:23
  • Have you tried namespacing each `UsersTableSeeder`. That way you could `call('local\UsersTableSeeder');` . The call method is looking for a class, not a file. – Jeemusu Jul 16 '14 at 01:37
  • I namespaced each `UsersTableSeeder` as `namespace seeds\local`, and then called it via `call('seeds\local\UsersTableSeeder')` and that also didn't work. – Kousha Jul 16 '14 at 01:50

3 Answers3

14

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.

judereid
  • 901
  • 9
  • 9
6

Laravel 5.7 or higher

if ( App::environment('local') ) {
        $this->call(Seeder::class);
}
if ( App::environment('production') ) {
            $this->call(Seeder::class);
}
if ( App::environment('testing') ) {
            $this->call(Seeder::class);
}
3

The problem is '/'. You should use '\' instead.

roottraveller
  • 7,942
  • 7
  • 60
  • 65