Last active
September 7, 2018 10:47
-
-
Save leantony/8deaccd90d156ae1ad8a8e1491673edf to your computer and use it in GitHub Desktop.
try this one
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
// base class | |
abstract class AbstractSeeder extends Command | |
{ | |
use TruncatesTables; | |
/** | |
* Create a new command instance. | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
$this->truncateRequiredTables(); | |
$this->insertData(); | |
} | |
/** | |
* Insert data into the database | |
* | |
* @return void | |
*/ | |
abstract protected function insertData(); | |
} | |
// the truncate functionality | |
trait TruncatesTables | |
{ | |
/** | |
* Truncates specified tables | |
* | |
* @return void | |
*/ | |
protected function truncateRequiredTables() | |
{ | |
if (property_exists($this, 'tables')) { | |
DB::statement("SET FOREIGN_KEY_CHECKS=0;"); | |
$this->info("Truncating tables " . json_encode($this->tables)); | |
foreach ($this->tables as $table) { | |
DB::table($table)->truncate(); | |
} | |
DB::statement("SET FOREIGN_KEY_CHECKS=1;"); | |
$this->info("Done truncating tables " . json_encode($this->tables)); | |
} | |
} | |
} | |
// a sample generator | |
// usage -> php artisan setup:houses | |
// it will truncate pre-determined tables for this generator | |
class GenerateHouses extends AbstractSeeder | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'setup:houses'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Sets up the db with houses.'; | |
/** | |
* Available tables that can be truncated | |
* | |
* @var array | |
*/ | |
protected $tables = [ | |
'houses', | |
'house_assignees', | |
'house_tenants', | |
'house_groups', | |
'house_types', | |
'house_rooms', | |
'house_room_types', | |
'house_clusters', | |
'house_cluster_types', | |
'house_rents', | |
'house_agreements', | |
'house_photos', | |
'house_tenants', | |
]; | |
/** | |
* Insert data into the database | |
* | |
* @return void | |
*/ | |
protected function insertData() | |
{ | |
// call a model factor(ies) here | |
$this->info('Setting up house cluster types....'); | |
factory(HouseClusterType::class, 5)->create(); | |
$this->info('Done setting house cluster types...'); | |
$this->info('Setting up house group types....'); | |
factory(HouseGroupType::class, 5)->create(); | |
$this->info('Done setting house group types...'); | |
$this->info('Setting up house types....'); | |
factory(HouseType::class, 5)->create(); | |
$this->info('Done setting house types...'); | |
$this->info('Setting up house room types....'); | |
factory(HouseRoomType::class, 5)->create(); | |
$this->info('Done setting house room types...'); | |
$this->info('Setting up house clusters....'); | |
factory(HouseCluster::class, 10)->create(); | |
$this->info('Done setting house clusters...'); | |
$this->info('Setting up house groups....'); | |
factory(HouseGroup::class, 10)->create(); | |
$this->info('Done setting house groups...'); | |
$this->info('Setting up houses....'); | |
factory(House::class, 100)->create(); | |
$this->info('Done setting houses...'); | |
$this->info('Setting up house rooms....'); | |
factory(HouseRoom::class, 200)->create(); | |
$this->info('Done setting house rooms...'); | |
$this->info('Setting up house photos....'); | |
factory(HousePhoto::class, 600)->create(); | |
$this->info('Done setting house photos...'); | |
$this->info('Setting up house assignments....'); | |
factory(HouseAssignee::class, 100)->create(); | |
$this->info('Done setting house assignments...'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment