From ae4af1675df8e9440943f9d764a722fa889aa9c9 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Thu, 8 Aug 2019 00:12:59 -0600 Subject: [PATCH] Add new commmand --- app/Console/Commands/ImportCities.php | 78 +++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 app/Console/Commands/ImportCities.php diff --git a/app/Console/Commands/ImportCities.php b/app/Console/Commands/ImportCities.php new file mode 100644 index 000000000..d61b20813 --- /dev/null +++ b/app/Console/Commands/ImportCities.php @@ -0,0 +1,78 @@ +error('Missing storage/app/cities.json file!'); + return; + } + + if(Place::count() > 10) { + $this->error('Cities already imported, aborting operation...'); + return; + } + $this->info('Importing city data into database ...'); + + $cities = file_get_contents($path); + $cities = json_decode($cities); + $count = count($cities); + $this->info("Found {$count} cities to insert ..."); + $bar = $this->output->createProgressBar($count); + $bar->start(); + + foreach ($cities as $city) { + $country = $city->country == 'XK' ? 'Kosovo' : (new \League\ISO3166\ISO3166)->alpha2($city->country)['name']; + DB::transaction(function () use ($city, $country) { + $place = new Place(); + $place->name = $city->name; + $place->slug = Str::slug($city->name); + $place->country = $country; + $place->lat = $city->lat; + $place->long = $city->lng; + $place->save(); + }); + $bar->advance(); + } + $bar->finish(); + return; + } +}