@TRAY2 - Ok, I think I have it.
class CreateStatesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('states', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('state');
});
}
class CreateCitiesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('cities', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('city');
$table->unsignedBigInteger('state_id');
$table->foreign('state_id')->references('id')->on('states');
$table->timestamps();
});
}
class CreateCountiesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('counties', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('county');
$table->timestamps();
});
}
class CreateZipCodesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('zipcodes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedMediumInteger('zipcode')->length(5);
$table->timestamps();
});
}
class CreateCityCountyPivotTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//
Schema::create('city_county', function(Blueprint $table)
{
$table->primary(['city_id', 'county_id']);
$table->unsignedBigInteger('city_id')->unsigned()->index();
$table->foreign('city_id')->references('id')->on('cities');
$table->unsignedBigInteger('county_id')->unsigned()->index();
$table->foreign('county_id')->references('id')->on('counties');
});
}
apologies on the formatting, I wish these code blocks would allow wrapping or overflow
class CreateCityZipcodePivotTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('city_zipcode', function (Blueprint $table) {
$table->primary(['city_id', 'zipcode_id']);
$table->unsignedBigInteger('city_id')->unsigned()->index();
$table->foreign('city_id')->references('id')->on('cities');
$table->unsignedBigInteger('zipcode_id')->unsigned()->index();
$table->foreign('zipcode_id')->references('id')->on('zipcodes');
});
}
class CreateStateZipcodePivotTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('state_zipcode', function (Blueprint $table) {
$table->primary(['state_id', 'zipcode_id']);
$table->unsignedBigInteger('state_id')->unsigned()->index();
$table->foreign('state_id')->references('id')->on('states');
$table->unsignedBigInteger('zipcode_id')->unsigned()->index();
$table->foreign('zipcode_id')->references('id')->on('zipcodes');
});
}
Here, I believe the model ZipCode belongsToMany states. Does that seem right, as zipcodes cross state lines? Or is hasmany states more appropriate?
class ZipCode extends Model
{
protected $fillable = ['zipcode'];
// belongsToMany
public function state()
{
return $this->belongsToMany(State::class);
}
// hasMany
public function cities()
{
return $this->hasMany(City::class);
}
public function counties()
{
return $this->hasMany(County::class);
}
}
class State extends Model
{
protected $fillable = ['state'];
// hasMany
public function cities()
{
return $this->hasMany(City::class);
}
public function zipcodes()
{
return $this->hasMany(ZipCode::class);
}
public function counties()
{
return $this->hasMany(County::class);
}
}
class City extends Model
{
protected $fillable = ['city'];
// belongsTo
public function state()
{
return $this->belongsTo(State::class);
}
// hasMany
public function counties()
{
return $this->hasMany(County::class);
}
public function zipcodes()
{
return $this->hasMany(ZipCode::class);
}
}
Putting in the data will be a nightmare, I can now see why @gn0rt0n offered up a package.
Does having zipcodes out of sorting order matter much? e.g. AZ is in the 85000 range. Should that data be placed in the database sequentially? Thank you `