Viewing file: LocationDatabaseSeeder.php (1.54 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace Database\Seeders;
use App\Models\Country; use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Seeder; use Illuminate\Support\Str;
class LocationDatabaseSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { Model::unguard();
$countriesCount = Country::count(); if (! $countriesCount) { $this->makeCountry(); } }
protected function makeCountry() { $countries_list = json_decode(file_get_contents(base_path('resources/backend/dummy-data/country.json')), true);
for ($i = 0; $i < count($countries_list); $i++) {
$country_data[] = [ 'name' => $countries_list[$i]['name'], 'sortname' => $countries_list[$i]['country_code'], 'slug' => Str::slug($countries_list[$i]['name']), 'image' => 'backend/image/flags/flag-of-'.str_replace(' ', '-', $countries_list[$i]['name'].'.jpg'), 'icon' => 'flag-icon-'.Str::lower($countries_list[$i]['country_code']), 'latitude' => $countries_list[$i]['latlng'][0], 'longitude' => $countries_list[$i]['latlng'][1], 'created_at' => Carbon::now(), 'updated_at' => Carbon::now(), ]; }
$country_chunks = array_chunk($country_data, ceil(count($country_data) / 3));
foreach ($country_chunks as $country) { Country::insert($country); } } }
|