Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 动态创建.env文件,然后迁移数据库并为其设置种子_Php_Database_Laravel_Laravel 5_Laravel 5.4 - Fatal编程技术网

Php 动态创建.env文件,然后迁移数据库并为其设置种子

Php 动态创建.env文件,然后迁移数据库并为其设置种子,php,database,laravel,laravel-5,laravel-5.4,Php,Database,Laravel,Laravel 5,Laravel 5.4,我正在尝试在Laravel5.4中编写一个控制台命令,该命令允许我动态创建一个.env文件,然后运行数据库迁移和种子程序 /** * Execute the console command. * * @return mixed */ public function handle() { // Check if we already have an .env file. if(!$this->envFileExists()) { // Create t

我正在尝试在Laravel5.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文件并生成和存储了applicationon密钥,但未能迁移数据库

[PDOException] SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: NO)
我假设这意味着应用程序在创建.env文件后没有读取该文件,即使它在正确的文件中创建了应用程序密钥

如果在.env文件已经存在之后再次运行该命令,则所有操作都会正确运行:数据库被迁移并设置种子。因此,很明显.env文件的创建是正确的,Laravel只是在初始安装时由于某种原因无法识别它


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

在迁移:安装之前调用
config:cache
命令
migrate:install

您是否尝试在“migrate:install”之前调用“config:cache”?由于框架已经启动,因此它不会“加载”您刚刚创建的env文件“config:cache”起了作用。如果你把它写成一个答案@MarcosKubis,我可以接受。非常感谢。谢谢@用户2759865