duannei0044
2017-02-14 14:06
浏览 88
已采纳

动态创建.env文件,然后迁移并播种数据库

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?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试在Laravel 5.4中编写一个控制台命令,这将允许我动态创建.env文件和 然后运行数据库迁移和播种器。

  / ** 
 *执行控制台命令。
 * 
 * @return mixed 
 * / 
 npublic function handle  (){
 //检查我们是否已有.env文件。
 if(!$ this-> envFileExists()){
 //创建.env文件
 $ this-> createEnvFile(  ); 
 $ this-> info('环境文件已成功创建。'); 
} 
 
 //生成应用程序密钥
 Artisan :: call('key:generate'); 
 $ this  - > info('应用程序密钥已成功生成。'); 
 
 //迁移
 Artisan :: call('migrate:install'); 
 $ this-> info('迁移表已成功创建。  '); 
 Artisan :: call('migrate:refresh'); 
 $ this-> info('所有表成功迁移。'); 
 
 // Seed 
 Artisan :: call('  db:seed'); 
 $ this-> info('所有表成功播种。'); 
}  
   
 
 

代码成功创建.env文件并生成并存储应用程序密钥,但无法迁移数据库。

  [PDOException] SQLSTATE [HY000] [1045]拒绝访问用户'forge'@'localhost'(使用密码:否)
   
 
 

其中我 '假设意味着应用程序在创建后不读取.env文件,即使它正确地在正确的文件中创建应用程序密钥。

如果我运行命令 第二次,在.env文件已经存在之后,一切都正常运行:数据库被迁移并播种。 很明显,.env文件正在被正确创建,并且Laravel在初始安装时由于某种原因而无法识别它。

如何强制Laravel使用新文件。 创建后的env文件?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongsu3664 2017-02-14 14:16
    已采纳

    Call config:cache command before migrate:install

    打赏 评论

相关推荐 更多相似问题