Last active
January 14, 2021 19:21
-
-
Save MuhammadSaim/f870811616b1d695f432216e4386e3be to your computer and use it in GitHub Desktop.
You wanna add countries in your laravel App just use this as a seeder
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 | |
/** | |
* Country Table Seeder | |
* @required Laravel 7+ | |
* @author Muhammad Saim | |
* Please make sure create a migration with these fields | |
* | |
* 1- iso | |
* 2- name | |
* 3- nicename | |
* 4- iso3 (This should be nullable) | |
* 5- numcode (This should be nullable) | |
* 6- phonecode | |
* | |
* | |
*/ | |
namespace Database\Seeders; | |
use App\Models\Country; | |
use Illuminate\Database\Seeder; | |
use Illuminate\Support\Facades\Http; | |
class CountryTableSeeder extends Seeder | |
{ | |
/** | |
* Run the database seeds. | |
* | |
* @return void | |
*/ | |
public function run() | |
{ | |
try{ | |
$countries = Http::retry(3, 100)->get("https://gist.githubusercontent.com/MuhammadSaim/41ebdc2f44fbc0625de4d73a372ce843/raw/c343fd2a81356cbab4c46ef6646332c3e91675f7/countries.json"); | |
$countries = $countries->json(); | |
foreach ($countries as $country){ | |
Country::create([ | |
'iso' => $country['iso'], | |
'name' => $country['name'], | |
'nicename' => $country['nicename'], | |
'iso3' => $country['iso3'], | |
'numcode' => $country['numcode'], | |
'phonecode' => $country['phonecode'], | |
]); | |
} | |
}catch (\Exception $e){ | |
throw new \Exception($e->getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment