LoginSignup
2
1

More than 5 years have passed since last update.

Laravel5.5でマイグレーション手順

Posted at

Laravelを使ってマイグレーション。テーブル更新時とかどうするんだっけ?とわからなくなるので、メモ。

  • create table
  • add column

テーブル作成

 1. マイグレーションファイル作成

まずは、

$ php artisan make:migration create_posts_table

とコマンドに入力してマイグレーションファイル作成。
※テーブル名は複数形を使った方が良さそう。

すると、database/migrations/に、{Y_m_d}_create_posts_table.phpが作成される。
この中のupメソッドに、作りたいテーブルの定義を書いていく。

 2. マイグレーション実行
マイグレーションファイルができたら、実行する

$ php artisan migrate

すると、データベースにテーブルができている。

カラム追加

  1. マイグレーションファイル作成

まずは、

$ php artisan make:migration add_column_comment --table=posts

とコマンドに入力してマイグレーションファイル作成。
すると、database/migrations/に、{Y_m_d}_add_column_comment.phpが作成される。
この中のupメソッドに、追加したいカラムの定義を書いていく。

 2. マイグレーション実行
マイグレーションファイルができたら、実行する

$ php artisan migrate

すると、テーブルにカラムが追加されている。

2
1
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
2
1