Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix creation of Countries Seeder for Laravel >= 8 #121

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/commands/MigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,32 @@ protected function createMigration()
}

//Create the seeder
$seeder_file = $this->laravel->path."/../database/seeds/CountriesSeeder.php";
$seeder_directory = $this->laravel->path."/../database/seeds";

$seeder_file = $seeder_directory."/CountriesSeeder.php";

$laravel_major_version = (int) app()->version();

$output = "<?php\n\n" .$app['view']->make('countries::generators.seeder')->render();

if($laravel_major_version >= 8){

$old_seeder_directory = $seeder_directory;

$seeder_directory = $this->laravel->path."/../database/seeders";

$seeder_file = $seeder_directory."/CountriesSeeder.php";

if(!is_dir($seeder_directory)){
/* Directory does not exist, rename seeds folder to seeders. */
rename($old_seeder_directory, $seeder_directory);
}

$output = "<?php\n\n" .$app['view']->make('countries::generators.seeder=>8')->render();
}



if (!file_exists( $seeder_file )) {
$fs = fopen($seeder_file, 'x');
if ($fs) {
Expand All @@ -135,4 +158,4 @@ public function fire()
{
$this->handle();
}
}
}
45 changes: 45 additions & 0 deletions src/views/generators/seeder=>8.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Countries;

class CountriesSeeder extends Seeder {

/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//Empty the countries table
DB::table(\Config::get('countries.table_name'))->delete();

//Get all of the countries
$countries = Countries::getList();
foreach ($countries as $countryId => $country){
DB::table(\Config::get('countries.table_name'))->insert(array(
'id' => $countryId,
'capital' => ((isset($country['capital'])) ? $country['capital'] : null),
'citizenship' => ((isset($country['citizenship'])) ? $country['citizenship'] : null),
'country_code' => $country['country-code'],
'currency' => ((isset($country['currency'])) ? $country['currency'] : null),
'currency_code' => ((isset($country['currency_code'])) ? $country['currency_code'] : null),
'currency_sub_unit' => ((isset($country['currency_sub_unit'])) ? $country['currency_sub_unit'] : null),
'currency_decimals' => ((isset($country['currency_decimals'])) ? $country['currency_decimals'] : null),
'full_name' => ((isset($country['full_name'])) ? $country['full_name'] : null),
'iso_3166_2' => $country['iso_3166_2'],
'iso_3166_3' => $country['iso_3166_3'],
'name' => $country['name'],
'region_code' => $country['region-code'],
'sub_region_code' => $country['sub-region-code'],
'eea' => (bool)$country['eea'],
'calling_code' => $country['calling_code'],
'currency_symbol' => ((isset($country['currency_symbol'])) ? $country['currency_symbol'] : null),
'flag' =>((isset($country['flag'])) ? $country['flag'] : null),
));
}
}
}