I'm trying to write a console command in Laravel 5.4 that will allow me to dynamically create a .env file and then run the database migrations and seeders.
/**
* Execute the console command.
*
* @return mixed
*/
public function handle() {
// Check if we already have an .env file.
if(!$this->envFileExists()) {
// Create the .env file
$this->createEnvFile();
$this->info('Environment file successfully created.');
}
// Generate application key
Artisan::call('key:generate');
$this->info('Application key successfully generated.');
// Migrate
Artisan::call('migrate:install');
$this->info('Migrations table successfully created.');
Artisan::call('migrate:refresh');
$this->info('All tables successfully migrated.');
// Seed
Artisan::call('db:seed');
$this->info('All tables successfully seeded.');
}
The code successfully creates the .env file AND generates and stores the applicaiton key, but fails to migrate the database.
[PDOException] SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: NO)
Which I'm assuming means the application is NOT reading the .env file after it has been created, even though it is creating the application key in the right file correctly.
If I run the command for a second time, after the .env file already exists, everything runs correctly: the database is migrated and seeded. So it is clear that the .env file is being created correctly and that Laravel is just not recognising it for some reason on it's initial install.
How can I force Laravel to use the new .env file after it has been created?