【问题标题】:Dynamically create .env file, then migrate and seed the database动态创建 .env 文件,然后迁移和种子数据库
【发布时间】:2017-02-14 14:06:07
【问题描述】:

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

/**
 * 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.');
}

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

[PDOException] SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: NO)

我假设这意味着应用程序在创建 .env 文件后没有读取它,即使它在正确的文件中正确创建了应用程序密钥。

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

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

【问题讨论】:

  • 您是否尝试在“migrate:install”之前调用“config:cache”?
  • 由于框架已经被引导,它不会“加载”你刚刚创建的 env 文件
  • 'config:cache' 成功了。如果您将其写为@MarcosKubis 的答案,我可以接受。谢谢!
  • 谢谢! @user2759865

标签: php database laravel laravel-5 laravel-5.4


【解决方案1】:

migrate:install之前调用config:cache命令

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 2013-10-20
    • 1970-01-01
    • 2017-04-08
    相关资源
    最近更新 更多