2017-02-14 14:06
浏览 88


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->info('Environment file successfully created.');

    // Generate application key
    $this->info('Application key successfully generated.');

    // Migrate
    $this->info('Migrations table successfully created.');
    $this->info('All tables successfully migrated.');

    // 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  (){
 if(!$ this-> envFileExists()){
 $ 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('所有表成功播种。'); 


  [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

    打赏 评论

相关推荐 更多相似问题