LaravelのMigrationでカラムの追加

# Laravelのデータベース設計を更新したい

最近、Laravelで新規のWebアプリケーションを製作しております。
MySQLを選択してMigrationでデータベースの設計をやっており、データの挿入をしていましたが、
追加したいカラムが出てきました。


```端末
mysql> desc stadium_posts;
+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| stadium    | varchar(255)     | NO   |     | NULL    |                |
| latitude   | double           | NO   |     | NULL    |                |
| longitude  | double           | NO   |     | NULL    |                |
| league     | varchar(255)     | NO   |     | NULL    |                |
| address    | varchar(255)     | NO   |     | NULL    |                |
| created_at | timestamp        | YES  |     | NULL    |                |
| updated_at | timestamp        | YES  |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+
8 rows in set (0.03 sec)


$ php artisan tinker
>>> App\StadiumPost::all()->toArray();
=> [
     [
       "id" => 1,
       "stadium" => "阪神甲子園球場",
       "latitude" => 34.721394,
       "longitude" => 135.361594,
       "league" => "プロ野球",
       "address" => "兵庫県西宮市甲子園町1番82号",
       "created_at" => "2019-02-02 10:37:56",
       "updated_at" => "2019-02-02 10:45:26",
     ],
     [
       "id" => 2,
       "stadium" => "東京ドーム",
       "latitude" => 35.703667,
       "longitude" => 139.753393,
       "league" => "プロ野球",
       "address" => "東京都文京区後楽１丁目３－６１",
       "created_at" => "2019-02-02 10:52:41",
       "updated_at" => "2019-02-02 10:52:41",
     ],
     [
       "id" => 3,
       "stadium" => "ナゴヤドーム",
       "latitude" => 35.170915,
       "longitude" => 136.881537,
       "league" => "プロ野球",
       "address" => "愛知県名古屋市東区大幸南1丁目1-1",
       "created_at" => "2019-02-02 11:05:36",
       "updated_at" => "2019-02-02 11:05:36",
     ],
     [
       "id" => 6,
       "stadium" => "明治神宮野球場",
       "latitude" => 35.674572,
       "longitude" => 139.717136,
       "league" => "プロ野球",
       "address" => "東京都新宿区霞ヶ丘町3番1号",
       "created_at" => "2019-02-02 11:09:29",
       "updated_at" => "2019-02-02 11:09:29",
     ],
     [
       "id" => 8,
       "stadium" => "横浜スタジアム",
       "latitude" => 35.443428,
       "longitude" => 139.6401,
       "league" => "プロ野球",
       "address" => "神奈川県横浜市中区横浜公園",
       "created_at" => "2019-02-02 11:11:30",
       "updated_at" => "2019-02-02 11:11:30",
     ],
     [
       "id" => 9,
       "stadium" => "MAZDA Zoom-Zoom スタジアム広島",
       "latitude" => 34.392938,
       "longitude" => 132.458942,
       "league" => "プロ野球",
       "address" => "広島県広島市中区本通８−２４",
       "created_at" => "2019-02-02 11:14:15",
       "updated_at" => "2019-02-02 11:14:15",
     ],
   ]
```

今のデータベースのカラムに以下のことをやりたいと思います。

-  カラムにスタジアムのある国の`country`を追加


#　Migrationファイルの作成・実行

参考にしたサイト
https://laraweb.net/knowledge/2251/

artisanコマンドでデータベース更新用のMigrationファイルを作成します

```端末
$ php artisan make:migration update_stadiumpost_table --table=StadiumPost
Created Migration: 2019_02_03_190732_update_stadiumpost_table
```

Migrationファイルを以下のように編集しました

```php:2019_02_03_190732_update_stadiumpost_table.php
<?php

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

class UpdateStadiumpostTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('stadium_posts', function (Blueprint $table) {
            // countryカラム追加
            $table->string('country');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('stadium_posts', function (Blueprint $table) {
            //
        });
    }
}
```

`php artisan migrate`でマイグレーションが実行され、以下の通りカラムが追加されました。

```
mysql> desc stadium_posts;
+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| stadium    | varchar(255)     | NO   |     | NULL    |                |
| latitude   | double           | NO   |     | NULL    |                |
| longitude  | double           | NO   |     | NULL    |                |
| league     | varchar(255)     | NO   |     | NULL    |                |
| address    | varchar(255)     | NO   |     | NULL    |                |
| created_at | timestamp        | YES  |     | NULL    |                |
| updated_at | timestamp        | YES  |     | NULL    |                |
| country    | varchar(255)     | NO   |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+
9 rows in set (0.01 sec)

$ php artisan tinker
>>> App\StadiumPost::all()->toArray();
=> [
     [
       "id" => 1,
       "stadium" => "阪神甲子園球場",
       "latitude" => "34.721394",
       "longitude" => "135.361594",
       "league" => "プロ野球",
       "address" => "兵庫県西宮市甲子園町1番82号",
       "created_at" => "2019-02-02 10:37:56",
       "updated_at" => "2019-02-02 10:45:26",
       "country" => "",
     ],
     [
       "id" => 2,
       "stadium" => "東京ドーム",
       "latitude" => "35.703667",
       "longitude" => "139.753393",
       "league" => "プロ野球",
       "address" => "東京都文京区後楽１丁目３－６１",
       "created_at" => "2019-02-02 10:52:41",
       "updated_at" => "2019-02-02 10:52:41",
       "country" => "",
     ],
     [
       "id" => 3,
       "stadium" => "ナゴヤドーム",
       "latitude" => "35.170915",
       "longitude" => "136.881537",
       "league" => "プロ野球",
       "address" => "愛知県名古屋市東区大幸南1丁目1-1",
       "created_at" => "2019-02-02 11:05:36",
       "updated_at" => "2019-02-02 11:05:36",
       "country" => "",
     ],
     [
       "id" => 6,
       "stadium" => "明治神宮野球場",
       "latitude" => "35.674572",
       "longitude" => "139.717136",
       "league" => "プロ野球",
       "address" => "東京都新宿区霞ヶ丘町3番1号",
       "created_at" => "2019-02-02 11:09:29",
       "updated_at" => "2019-02-02 11:09:29",
       "country" => "",
     ],
     [
       "id" => 8,
       "stadium" => "横浜スタジアム",
       "latitude" => "35.443428",
       "longitude" => "139.6401",
       "league" => "プロ野球",
       "address" => "神奈川県横浜市中区横浜公園",
       "created_at" => "2019-02-02 11:11:30",
       "updated_at" => "2019-02-02 11:11:30",
       "country" => "",
     ],
     [
       "id" => 9,
       "stadium" => "MAZDA Zoom-Zoom スタジアム広島",
       "latitude" => "34.392938",
       "longitude" => "132.458942",
       "league" => "プロ野球",
       "address" => "広島県広島市中区本通８−２４",
       "created_at" => "2019-02-02 11:14:15",
       "updated_at" => "2019-02-02 11:14:15",
       "country" => "",
     ],
   ]
```