Hi all, I looked around and it seems that seeding countries/states and cities if still a pain if you want to also setup relationships between them but this is more of a Laravel problem and not really an Eloquent one.
I'm using this package: https://github.com/khsing/laravel-world
I want to pluck the names of each country, state (if applicable) and city and use it to seed my db since I want to use my own models and not the ones from the package. I am also using the methods provided by the package to set the relations between cities, states, and countries with my own models.
It worked perfectly for the seeding the Countries but I am having issues for states and cities.
When seeding the states, the foreach loop seems to break and just create one state per country (1x soutch korea, 1x USA etc). The result actually does create a correct relationship between the state and the country but once that is done it skips to the next.
I hope you guys can help me figure out what is not working here!
A few notes about the package I used:
//returns all countries
use Khsing\World\World;
World::Countries()
//in this example this gets China and then grabs all of the regions with the children() method.
//If a country does not have states/regions, has_division returns false
$china = Country::getByCode('cn');
// check has_division to determine next level is division or city.
$china->has_division; // true, otherwise is false
$regsions = $china->children();
And here is the code for the seeder I wrote:
...
use App\Models\State;
use Illuminate\Database\Seeder;
use Khsing\World\World;
use App\Models\Country;
use App\Models\City;
use Illuminate\Database\Eloquent\Model;
class StateSeeder extends Seeder
{
public function run()
{
$countriesname= World::Countries();
foreach($countriesname as $countryname){
if($countryname->has_division){
foreach($countryname->children() as $statename)
$state = new State;
$state->name = $statename->name;
$country= Country::where('name', $countryname->name )->first();
$state->country()->associate($country);
$state->save();
}
}
}
}
As for the migration tables
...
class CreateCitiesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('cities', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('country_id')->unsigned();
$table->integer('state_id')->nullable()->unsigned();
$table->timestamps();
});
}
...
As for the States Table
class CreateStatesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('states', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->foreignId('country_id')->constrained()->onDelete('cascade');
$table->timestamps();
});
}
As for the countries Table
class CreateCountriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('countries', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
...