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

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

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?