Created
April 1, 2020 11:09
-
-
Save jpswade/395a1ffd4c710930437e16c1664ad4ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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