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 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教