动态创建 .env 文件,然后迁移和播种数据库
Dynamically create .env file, then migrate and seed the database
我正在尝试在 Laravel 5.4 中编写一个控制台命令,它允许我动态创建一个 .env 文件,然后 运行 数据库迁移和播种器。
/**
* 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.');
}
代码成功创建 .env 文件并生成并存储应用程序密钥,但未能迁移数据库。
[PDOException] SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: NO)
我假设这意味着应用程序在创建后不会读取 .env 文件,即使它在正确的文件中正确创建应用程序密钥。
如果我第二次 运行 命令,在 .env 文件已经存在之后,一切都 运行 正确:数据库被迁移和播种。因此很明显,.env 文件正在正确创建,并且 Laravel 只是在初始安装时出于某种原因无法识别它。
如何强制 Laravel 在创建新的 .env 文件后使用它?
在migrate:install
之前调用config:cache
命令
我正在尝试在 Laravel 5.4 中编写一个控制台命令,它允许我动态创建一个 .env 文件,然后 运行 数据库迁移和播种器。
/**
* 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.');
}
代码成功创建 .env 文件并生成并存储应用程序密钥,但未能迁移数据库。
[PDOException] SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: NO)
我假设这意味着应用程序在创建后不会读取 .env 文件,即使它在正确的文件中正确创建应用程序密钥。
如果我第二次 运行 命令,在 .env 文件已经存在之后,一切都 运行 正确:数据库被迁移和播种。因此很明显,.env 文件正在正确创建,并且 Laravel 只是在初始安装时出于某种原因无法识别它。
如何强制 Laravel 在创建新的 .env 文件后使用它?
在migrate:install
config:cache
命令