Skip to content

Instantly share code, notes, and snippets.

@jpswade
Created April 1, 2020 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpswade/395a1ffd4c710930437e16c1664ad4ec to your computer and use it in GitHub Desktop.
Save jpswade/395a1ffd4c710930437e16c1664ad4ec to your computer and use it in GitHub Desktop.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class DatabaseCreateCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'db:create';
/**
* The console command description.
*
* @var string
*/
protected $description = 'This command creates a new database';
/**
* The console command signature.
*
* @var string
*/
protected $signature = 'db:create';
/**
* Execute the console command.
*/
public function fire()
{
$connectionName = config('database.default');
$connection = config('database.connections')[$connectionName];
$schemaName = $connection['database'];
if (!$schemaName) {
throw new \DomainException('Missing Database Name');
}
$query = sprintf(
'CREATE DATABASE IF NOT EXISTS %s CHARACTER SET %s COLLATE %s;',
$schemaName,
$connection['charset'],
$connection['collation']
);
config(["database.connections.{$connectionName}.database" => null]);
DB::statement($query);
config(["database.connections.{$connectionName}.database" => $schemaName]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment