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.