Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have different seeders per environments in Laravel 4?

I have a simple question but I haven't found an answer in the web. Maybe my keywords are false.

So I am developing an app in Laravel 4. And I need to seed the database with different values according to the current active environment.

So for example, if I am on the local environment, I want to have test data and so on. But when I am on the production environment I only want to have an admin user.

Does Laravel has an built in solution for that?

If not, how can check, which environment is active in the app/seeds/DatabaseSeeder.php file. So I can call different seeder according to the environment.

like image 337
Reflic Avatar asked Jul 09 '13 19:07

Reflic


1 Answers

There's no built-in handler for different environments in the manner you would like.

Solution

Within the seeder class, you should be able to use App::environment() to detect the environment, and do logic based on that.

You can add that within each table seeder class, or within the DatabaseSeeder.php file:

public function run()
{
    Eloquent::unguard();

    if( App::environment() === 'development' )
    {
        $this->call('UserTableSeeder');
    } 
}

Alternatively

Consider adding multiple database connections within your app/config/database.php file. That way, instead of seeding per environments, you can populate databases from multiple connections within the same environment (and the environment can still change but have 2 or more separate db connections).

If that fits your use case, see my answer on multiple database connections here.

like image 158
fideloper Avatar answered Sep 29 '22 07:09

fideloper