How to use different seeders for each environment in Laravel 4?

I have a simple question, but I did not find an answer on the Internet. Perhaps my keywords are false.

So, I am developing an application in Laravel 4. And I need to sow the database with different values ​​in accordance with the current active environment.

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

Does Laravel have a built-in solution for this?

If not, how to check which environment is active in the app/seeds/DatabaseSeeder.php file. Therefore, I can name another seeder according to the environment.

+4
source share
1 answer

There is no built-in handler for different environments, as you would like.

Decision

In the seeder class, you can use App :: environment () to detect the environment and execute logic based on what.

You can add that in each class of the table sower or in the DatabaseSeeder.php file:

 public function run() { Eloquent::unguard(); if( App::environment() === 'development' ) { $this->call('UserTableSeeder'); } } 

As an alternative

Consider adding multiple database connections in the app/config/database.php file. Thus, instead of seeding in environments, you can populate databases from multiple connections in the same environment (and the environment can still change, but have two or more separate db connections).

If this matches your use case, see my answer for a few database connections here .

+9
source

Source: https://habr.com/ru/post/1490551/


All Articles