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.
There's no built-in handler for different environments in the manner you would like.
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');
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With