Laravelで使える地域と都道府県のseeder

# はじめに
今回やるプロジェクトで、地域と都道府県のseederを作りまして、わりと頻繁に使いそうなので載せます。

:::note warn
実運用で使う場合は、データ間違いないか自己責任でご確認ください！！
（まだテストしておりませんので）
:::

ちなみにマイグレーションファイルはありませんので、自分で作ってね。

特定のseederだけ実行したいときのコマンド
```
$ php artisan db:seed --class=AreaSeeder
```

# 地域テーブルのseeder
```php
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class AreaSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        \DB::table('areas')->truncate();

        $data = [
            [
                'name' => '北海道',
                'slug' => 'hokkaido',
            ],
            [
                'name' => '東北',
                'slug' => 'tohoku',
            ],
            [
                'name' => '関東',
                'slug' => 'kanto',
            ],
            [
                'name' => '甲信越・北陸',
                'slug' => 'koshinetsu',
            ],
            [
                'name' => '東海',
                'slug' => 'tokai',
            ],
            [
                'name' => '関西',
                'slug' => 'kansai',
            ],
            [
                'name' => '中国',
                'slug' => 'chugoku',
            ],
            [
                'name' => '四国',
                'slug' => 'shikoku',
            ],
            [
                'name' => '九州・沖縄',
                'slug' => 'kyusyu',
            ],
        ];

        foreach($data as $d) {
            \DB::table('areas')->insert($d);
        }
    }
}

```

# 都道府県テーブルのseeder
```php
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class PrefectureSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        \DB::table('prefectures')->truncate();

        $hokkaido = [
            'area_id' => 1,
            'name' => '北海道',
            'slug' => 'hokkaido',
        ];

        \DB::table('prefectures')->insert($hokkaido);

        $tohoku = [
            [
                'area_id' => 2,
                'name' => '青森県',
                'slug' => 'aomori',
            ],
            [
                'area_id' => 2,
                'name' => '岩手県',
                'slug' => 'iwate',
            ],
            [
                'area_id' => 2,
                'name' => '宮城県',
                'slug' => 'miyagi',
            ],
            [
                'area_id' => 2,
                'name' => '秋田県',
                'slug' => 'akita',
            ],
            [
                'area_id' => 2,
                'name' => '山形県',
                'slug' => 'yamagata',
            ],
            [
                'area_id' => 2,
                'name' => '福島県',
                'slug' => 'fukushima',
            ],
        ];

        foreach($tohoku as $d) {
            \DB::table('prefectures')->insert($d);
        }

        $kanto = [
            [
                'area_id' => 3,
                'name' => '茨城県',
                'slug' => 'ibaraki',
            ],
            [
                'area_id' => 3,
                'name' => '栃木県',
                'slug' => 'tochigi',
            ],
            [
                'area_id' => 3,
                'name' => '群馬県',
                'slug' => 'gunma',
            ],
            [
                'area_id' => 3,
                'name' => '埼玉県',
                'slug' => 'saitama',
            ],
            [
                'area_id' => 3,
                'name' => '千葉県',
                'slug' => 'chiba',
            ],
            [
                'area_id' => 3,
                'name' => '東京都',
                'slug' => 'tokyo',
            ],
            [
                'area_id' => 3,
                'name' => '神奈川県',
                'slug' => 'kanagawa',
            ],
        ];

        foreach($kanto as $d) {
            \DB::table('prefectures')->insert($d);
        }


        $koshinetsu = [
            [
                'area_id' => 4,
                'name' => '新潟県',
                'slug' => 'niigata',
            ],
            [
                'area_id' => 4,
                'name' => '富山県',
                'slug' => 'toyama',
            ],
            [
                'area_id' => 4,
                'name' => '石川県',
                'slug' => 'ishikawa',
            ],
            [
                'area_id' => 4,
                'name' => '福井県',
                'slug' => 'fukui',
            ],
            [
                'area_id' => 4,
                'name' => '山梨県',
                'slug' => 'yamanashi',
            ],
            [
                'area_id' => 4,
                'name' => '長野県',
                'slug' => 'nagano',
            ],
        ];

        foreach($koshinetsu as $d) {
            \DB::table('prefectures')->insert($d);
        }


        $tokai = [
            [
                'area_id' => 5,
                'name' => '岐阜県',
                'slug' => 'gifu',
            ],
            [
                'area_id' => 5,
                'name' => '静岡県',
                'slug' => 'shizuoka',
            ],
            [
                'area_id' => 5,
                'name' => '愛知県',
                'slug' => 'aichi',
            ],
            [
                'area_id' => 5,
                'name' => '三重県',
                'slug' => 'mie',
            ],
        ];

        foreach($tokai as $d) {
            \DB::table('prefectures')->insert($d);
        }


        $kansai = [
            [
                'area_id' => 6,
                'name' => '滋賀県',
                'slug' => 'shiga',
            ],
            [
                'area_id' => 6,
                'name' => '京都府',
                'slug' => 'kyoto',
            ],
            [
                'area_id' => 6,
                'name' => '大阪府',
                'slug' => 'osaka',
            ],
            [
                'area_id' => 6,
                'name' => '兵庫県',
                'slug' => 'hyogo',
            ],
            [
                'area_id' => 6,
                'name' => '奈良県',
                'slug' => 'nara',
            ],
            [
                'area_id' => 6,
                'name' => '和歌山県',
                'slug' => 'wakayama',
            ],
        ];

        foreach($kansai as $d) {
            \DB::table('prefectures')->insert($d);
        }


        $chugoku = [
            [
                'area_id' => 7,
                'name' => '鳥取県',
                'slug' => 'tottori',
            ],
            [
                'area_id' => 7,
                'name' => '島根県',
                'slug' => 'shimane',
            ],
            [
                'area_id' => 7,
                'name' => '岡山県',
                'slug' => 'okayama',
            ],
            [
                'area_id' => 7,
                'name' => '広島県',
                'slug' => 'hiroshima',
            ],
            [
                'area_id' => 7,
                'name' => '山口県',
                'slug' => 'yamaguchi',
            ],
        ];

        foreach($chugoku as $d) {
            \DB::table('prefectures')->insert($d);
        }


        $shikoku = [
            [
                'area_id' => 8,
                'name' => '徳島県',
                'slug' => 'tokushima',
            ],
            [
                'area_id' => 8,
                'name' => '香川県',
                'slug' => 'kagawa',
            ],
            [
                'area_id' => 8,
                'name' => '愛媛県',
                'slug' => 'ehime',
            ],
            [
                'area_id' => 8,
                'name' => '高知県',
                'slug' => 'kouchi',
            ],
        ];


        foreach($shikoku as $d) {
            \DB::table('prefectures')->insert($d);
        }

        $kyusyu = [
            [
                'area_id' => 9,
                'name' => '福岡県',
                'slug' => 'fukuoka',
            ],
            [
                'area_id' => 9,
                'name' => '佐賀県',
                'slug' => 'saga',
            ],
            [
                'area_id' => 9,
                'name' => '長崎県',
                'slug' => 'nagasaki',
            ],
            [
                'area_id' => 9,
                'name' => '熊本県',
                'slug' => 'kumamoto',
            ],
            [
                'area_id' => 9,
                'name' => '大分県',
                'slug' => 'ooita',
            ],
            [
                'area_id' => 9,
                'name' => '宮崎県',
                'slug' => 'miyazaki',
            ],
            [
                'area_id' => 9,
                'name' => '鹿児島県',
                'slug' => 'kagoshima',
            ],
            [
                'area_id' => 9,
                'name' => '沖縄県',
                'slug' => 'okinawa',
            ],
        ];


        foreach($kyusyu as $d) {
            \DB::table('prefectures')->insert($d);
        }
    }
}

```

# 最後に
[地方・都道府県マスタ](https://qiita.com/chiyoyo/items/51891a8956e03422dc88)
この記事も参考にしました。
結構、需要あるよね。

[都道府県一覧をコピペ ！テキスト、配列、連想配列、セレクトボックス用など](https://yutaihara.com/archives/80)
このページも都道府県のセレクトボックスで使えますね！