LoginSignup
1
0

More than 1 year has passed since last update.

既存テーブルにカラムを追加するには

Posted at

都道府県を選んだときに、選んだ都道府県に応じた市区町村が表示されるような、
都道府県と市区町村の連動をするための、カラム追加を行います。

具体的には、citiesテーブルにprefecture_idを追加します。

php artisan make:migration add_prefecture_id_to_cities_table --table=cities

下記コマンドの()内が、状況に応じて入る値が変化するところです。
php artisan make:migration add_(追加するカラム名)to(追加するテーブル名)_table --table=(紐づけたい既存のテーブル名)

◆注意点◆
コマンド後半部分の、cities_tableの、tableと記載することを忘れてしまいがちなので、
ここは個人的に注意したいと思います。

その後は、php artisan migrateを実行。

最後に、citiesテーブルにprefecture_idが入っているかを確認します。

sqlite> PRAGMA TABLE_INFO('cities');
0|id|integer|1||1
1|name|varchar|1||0
2|created_at|datetime|0||0
3|updated_at|datetime|0||0
4|prefecture_id|integer|0||0

見事、入っていました!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0