I would like to generate a database table, migrate and seed on the fly based using my .env file
So to be clear. What I'd love to see is an Artisan command called: db:make. Using the .env file to create all the need elements. Is it possible, with the current setup of Laravel 5.3 to do this? So after watching the artisan command laracast, i'm feeling confident that i can, but then start to realize that the complexity of the problem. Which I'll lay out now.
So the first thing that I start to realize is all I know is mysql. I question whether it will work with pgsql, sqlite, others. I don't know. Ok so right off the bat is this a good idea, thinking Taylor must have had a reason, the way he did it? But I keep moving forward on it, thinking I'll just see if mysql can do it. The other thing is it's not very "Laravely". No DB::class facade. which makes it all fall apart real quick, stuck with just mysql.
Is there a reason Taylor didn't go this route, that i'm not seeing?
//.env
DB_CONNECTION=mysql
DB_HOST=0.0.0.0 //might be localhost or remote site might not
DB_PORT=3306
DB_DATABASE=mytable
DB_USERNAME=root
DB_PASSWORD=root
//App\Console\Commands\DatabaseMake.php
...
protected $signature = 'db:make {--force}';
protected $description = 'Create Database from .env file';
...
public function handle() {
// Create connection
$this->info('Connect to Database');
//Didn't know how to connect with DB facade? Can it?
$mysql = new \mysqli($host, env("DB_USERNAME"), env("DB_PASSWORD"));
// Check connection
if($mysql->connect_error) {
$this->error("Connection failed: " . $mysql->connect_error);
}
// Allow option to force if the db already exists.
if($this->option('force')) {
$sql = "DROP DATABASE " . env("DB_DATABASE");
$mysql->query($sql);
}
// Create database
$sql = "CREATE DATABASE " . env("DB_DATABASE");
if($mysql->query($sql) === TRUE) {
$this->info("Create Database: " . env("DB_DATABASE") . " successfully");
$this->info('Migrate');
\Artisan::call('migrate');
$this->info('Seed');
\Artisan::call('db:seed');
} else {
$this->info("Error creating database: " . $mysql->error);
}
$mysql->close();
}
It's kind of like the 'Elixir' principle, I know how to create a database and i'm sure others do, for me it's just more mind clutter that I'd like to not worry about. but would like to stay as true to the Laravel way.
Thanks all Dustin
Please or to participate in this conversation.