Laravelで八地方区分・都道府県テーブル作成

#はじめに
個人アプリを作る際に、ググってもぴったりの記事が出てこなかったのでメモとして書き留めておきます。
環境に合わせてお使いください。😎✨

#Migrationファイル作成
```:八地方区分テーブル
$php artisan make:migrate create_regions_table --create=regions
```
```:都道府県テーブル
$php artisan make:migrate create_prefectures_table --create=prefectures
```

#Migrationファイル修正
###八地方区分テーブル
```php:database/migrations/2019_05_20_113003_create_regions_table.php
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateRegionsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('regions', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('regions');
    }
}
```
###都道府県テーブル
```php:database/migrations/2019_05_20_113003_create_regions_table.php
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePrefecturesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('prefectures', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('region_id')->unsigned();
            $table->integer('code');
            $table->string('name');
            $table->foreign('region_id')->references('id')->on('regions'); //外部キー制約
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('prefectures');
    }
}
```
※migrationファイルの作成順序（親テーブルを作ってから子テーブル）


#Seedファイル作成
```:八地方区分テーブル
$php artisan make:seeder RegionsSeeder
```
```:都道府県テーブル
$php artisan make:seeder PrefecturesSeeder
```

#Seedファイル修正
##八地方区分seeder
```php:database/seeds/RegionsSeeder.php
<?php

use Illuminate\Database\Seeder;

class RegionsSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('regions')->insert([
            ['name' => '北海道地方'],
            ['name' => '東北地方'],
            ['name' => '関東地方'],
            ['name' => '中部地方'],
            ['name' => '近畿地方'],
            ['name' => '中国地方'],
            ['name' => '四国地方'],
            ['name' => '九州地方'],
        ]);
    }
}
```
##都道府県seeder
```php:database/seeds/PrefecturesSeeder.php
<?php

use Illuminate\Database\Seeder;

class PrefecturesSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('prefectures')->insert([
            ['region_id' => 1, 'code' => 1, 'name' => '北海道'],
            ['region_id' => 2, 'code' => 2, 'name' => '青森県'],
            ['region_id' => 2, 'code' => 3, 'name' => '岩手県'],
            ['region_id' => 2, 'code' => 4, 'name' => '宮城県'],
            ['region_id' => 2, 'code' => 5, 'name' => '秋田県'],
            ['region_id' => 2, 'code' => 6, 'name' => '山形県'],
            ['region_id' => 2, 'code' => 7, 'name' => '福島県'],
            ['region_id' => 3, 'code' => 8, 'name' => '茨城県'],
            ['region_id' => 3, 'code' => 9, 'name' => '栃木県'],
            ['region_id' => 3, 'code' => 10, 'name' => '群馬県'],
            ['region_id' => 3, 'code' => 11, 'name' => '埼玉県'],
            ['region_id' => 3, 'code' => 12, 'name' => '千葉県'],
            ['region_id' => 3, 'code' => 13, 'name' => '東京都'],
            ['region_id' => 3, 'code' => 14, 'name' => '神奈川県'],
            ['region_id' => 4, 'code' => 15, 'name' => '新潟県'],
            ['region_id' => 4, 'code' => 16, 'name' => '富山県'],
            ['region_id' => 4, 'code' => 17, 'name' => '石川県'],
            ['region_id' => 4, 'code' => 18, 'name' => '福井県'],
            ['region_id' => 4, 'code' => 19, 'name' => '山梨県'],
            ['region_id' => 4, 'code' => 20, 'name' => '長野県'],
            ['region_id' => 4, 'code' => 21, 'name' => '岐阜県'],
            ['region_id' => 4, 'code' => 22, 'name' => '静岡県'],
            ['region_id' => 4, 'code' => 23, 'name' => '愛知県'],
            ['region_id' => 5, 'code' => 24, 'name' => '三重県'],
            ['region_id' => 5, 'code' => 25, 'name' => '滋賀県'],
            ['region_id' => 5, 'code' => 26, 'name' => '京都府'],
            ['region_id' => 5, 'code' => 27, 'name' => '大阪府'],
            ['region_id' => 5, 'code' => 28, 'name' => '兵庫県'],
            ['region_id' => 5, 'code' => 29, 'name' => '奈良県'],
            ['region_id' => 5, 'code' => 30, 'name' => '和歌山県'],
            ['region_id' => 6, 'code' => 31, 'name' => '鳥取県'],
            ['region_id' => 6, 'code' => 32, 'name' => '島根県'],
            ['region_id' => 6, 'code' => 33, 'name' => '岡山県'],
            ['region_id' => 6, 'code' => 34, 'name' => '広島県'],
            ['region_id' => 6, 'code' => 35, 'name' => '山口県'],
            ['region_id' => 7, 'code' => 36, 'name' => '徳島県'],
            ['region_id' => 7, 'code' => 37, 'name' => '香川県'],
            ['region_id' => 7, 'code' => 38, 'name' => '愛媛県'],
            ['region_id' => 7, 'code' => 39, 'name' => '高知県'],
            ['region_id' => 8, 'code' => 40, 'name' => '福岡県'],
            ['region_id' => 8, 'code' => 41, 'name' => '佐賀県'],
            ['region_id' => 8, 'code' => 42, 'name' => '長崎県'],
            ['region_id' => 8, 'code' => 43, 'name' => '熊本県'],
            ['region_id' => 8, 'code' => 44, 'name' => '大分県'],
            ['region_id' => 8, 'code' => 45, 'name' => '宮崎県'],
            ['region_id' => 8, 'code' => 46, 'name' => '鹿児島県'],
            ['region_id' => 8, 'code' => 47, 'name' => '沖縄県'],
        ]);
    }
```
#シーダークラスを実行する為の設定
```php:database/seeds/DatabaseSeeder.php
<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(RegionsSeeder::class);
        $this->call(PrefecturesSeeder::class);
    }
}
```

#DBにテーブル登録&シーディング実行
```
$php artisan migrate:refresh --seed
```
以上で、終了です🎉