Last active
September 17, 2015 13:57
-
-
Save MwirabuaTimothy/5c7a450469b90740adde to your computer and use it in GitHub Desktop.
Artisan command that combines dropping, creating, migrating and seeding database
This file contains 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; | |
class AppInstall extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'app:install'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Setting up your app for the first time.'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
$this->comment('====================================='); | |
$this->comment(''); | |
$this->comment('Preparing your application...'); | |
$this->comment(''); | |
$this->call('db:drop'); | |
$this->call('db:create'); | |
$this->askGenerateKey(); | |
$this->askInstallMigrate(); | |
} | |
protected function askGenerateKey() | |
{ | |
// Ask the user whether to generate key | |
$ans = $this->ask('Generate App key?: ', 'y'); | |
// Check if the answer is true | |
if (preg_match('/^y/', $ans)) | |
{ | |
// Generate the Application Encryption key | |
$this->call('key:generate'); | |
} | |
} | |
protected function askInstallMigrate() | |
{ | |
// Ask the user whether to run the resr | |
$ans = $this->ask('Create "migrations" table?: ', 'y'); | |
// Check if the answer is true | |
if (preg_match('/^y/', $ans)) | |
{ | |
// Create the migrations table | |
$this->call('migrate:install'); | |
// Run the Migrations | |
$this->call('migrate'); | |
// Run database seeding | |
$this->seedDB(); | |
} | |
} | |
protected function seedDB() | |
{ | |
// Ask the user whether to run the resr | |
$ans = $this->ask('Seed your database?: ', 'y'); | |
// Check if the answer is true | |
if (preg_match('/^y/', $ans)) | |
{ | |
$this->call('db:seed'); | |
$this->comment(''); | |
$this->comment('Database successfully seeded.'); | |
$this->comment('====================================='); | |
echo "Your app is now ready!"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment