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

动态创建.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?

  • 写回答

1条回答 默认 最新

  • dongsu3664 2017-02-14 14:16
    关注

    Call config:cache command before migrate:install

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了