dragonfly9527 2014-08-12 22:44 采纳率: 100%
浏览 523

Laravel在动态添加数据库名称后刷新数据库配置缓存

I am trying to create a web user interface in which a user specifies the name of a new database, then that database is created, the Laravel database config file is updated and migrations are ran.

Don't worry I'm going to clean up this code, I'm just first trying to get the basic functionality down.

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));

    }

The database is successfully created, the config/database.php file is updated, and the migrations attempt to run, but I get this error:

{"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}}

I don't know how to refresh the database connection that seems to be cached. Any advice would be great, I've been stuck on this for a few hours now.

  • 写回答

1条回答 默认 最新

  • douju6542 2014-08-13 00:55
    关注

    Got this to work using system() instead of Artisan::call()

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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?