【问题标题】:Laravel refreshing database config cache after dynamically adding database nameLaravel 动态添加数据库名称后刷新数据库配置缓存
【发布时间】:2014-08-12 22:44:28
【问题描述】:

我正在尝试创建一个 Web 用户界面,其中用户指定新数据库的名称,然后创建该数据库,更新 Laravel 数据库配置文件并运行迁移。

别担心,我会清理这段代码,我只是先尝试​​降低基本功能。

public function setupDatabase($input)
    {

        /* set the database name variable from user defined input */
        $dbname = $input['database'];

        /* create the database from input */
        DB::statement("CREATE DATABASE IF NOT EXISTS `{$dbname}`");

        /* update laravels database config file to the new database name */
        $writeConfig = new \October\Rain\Config\Rewrite;
        $writeConfig->toFile(app_path() . '/config/database.php', [
            'connections.mysql.database' => $dbname
        ]);

        /* run migrations */
        Artisan::call('migrate', array('--force' => true));

    }

数据库已成功创建,config/database.php 文件已更新,迁移尝试运行,但出现此错误:

{"error":{"type":"Illuminate\\Database\\QueryException","message":"SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected (SQL: create table `migrations` (`migration` varchar(255) not null, `batch` int not null) default character set utf8 collate utf8_unicode_ci)","file":"\/test\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Connection.php","line":600}}

我不知道如何刷新似乎被缓存的数据库连接。任何建议都会很棒,我已经坚持了几个小时了。

【问题讨论】:

    标签: php mysql laravel laravel-4


    【解决方案1】:

    使用 system() 代替 Artisan::call() 使其工作

    system('cd path/to/project && php artisan migrate --force');
    

    【讨论】:

      猜你喜欢
      • 2012-01-29
      • 1970-01-01
      • 2017-01-05
      • 2012-05-12
      • 2012-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多