Skip to content

Instantly share code, notes, and snippets.

@coolsam726
Created December 8, 2021 13:02
Show Gist options
  • Save coolsam726/1ba1fbc5c1015eebfc312b192716d3c9 to your computer and use it in GitHub Desktop.
Save coolsam726/1ba1fbc5c1015eebfc312b192716d3c9 to your computer and use it in GitHub Desktop.
Kenya Counties Laravel Seeder
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class CountiesSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$counties = [
1 => "Mombasa",
2 => "Kwale",
3 => "Kilifi",
4 => "Tana River",
5 => "Lamu",
6 => "Taita/Taveta",
7 => "Garissa",
8 => "Wajir",
9 => "Mandera",
10 => "Marsabit",
11 => "Isiolo",
12 => "Meru",
13 => "Tharaka-Nithi",
14 => "Embu",
15 => "Kitui",
16 => "Machakos",
17 => "Makueni",
18 => "Nyandarua",
19 => "Nyeri",
20 => "Kirinyaga",
21 => "Murang'a",
22 => "Kiambu",
23 => "Turkana",
24 => "West Pokot",
25 => "Samburu",
26 => "Trans Nzoia",
27 => "Uasin Gishu",
28 => "Elgeyo/Marakwet",
29 => "Nandi",
30 => "Baringo",
31 => "Laikipia",
32 => "Nakuru",
33 => "Narok",
34 => "Kajiado",
35 => "Kericho",
36 => "Bomet",
37 => "Kakamega",
38 => "Vihiga",
39 => "Bungoma",
40 => "Busia",
41 => "Siaya",
42 => "Kisumu",
43 => "Homa Bay",
44 => "Migori",
45 => "Kisii",
46 => "Nyamira",
47 => "Nairobi City",
];
$kenya = \DB::table('countries')->where("code","=","KE")->first();
if ($kenya) {
$kenyaId = $kenya->id;
} else {
$kenyaId = null;
}
$countiesData = collect($counties)->map(function ($county, $key) use($kenyaId) {return [
"id" => $key,
"name" => $county,
"country_id" => $kenyaId,
"created_at" => now(),
"updated_at" => now()
];});
\Schema::disableForeignKeyConstraints();
\DB::table('counties')->delete();
\DB::table('counties')->truncate();
\DB::table('counties')->insert($countiesData->toArray());
\Schema::enableForeignKeyConstraints();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment