Warning: Undefined array key 1 in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 12
Warning: Undefined array key 2 in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 15
Warning: Undefined array key 3 in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 18
Warning: Undefined array key 4 in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 21
Warning: Undefined array key 1 in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 24
Warning: Undefined array key 2 in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 27
Warning: Undefined array key 3 in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 30
Warning: Undefined array key 4 in /home/naoooooo8/naoya-ono.com/public_html/blog/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 33
このページでは、タイトルの通り「マイグレーション機能を使ってテーブルのカラムを追加する方法」について解説します。
テーブルのカラム追加手順
カラム追加の手順は次の通り。
- マイグレーションファイルを作成
- マイグレーションファイルにカラム追加の処理を記述
- マイグレーションの実行
それぞれ順に解説していきます。
1. マイグレーションファイルを作成
php artisan make:migration 任意のファイル名 --table=テーブル名
ファイルは/database/migrations配下に生成される。
マイグレーションファイル名は任意でOKですが、ファイル名がクラス名になるので、、できるだけわかりやすいものがいいでしょう。
例えば、商品テーブルにカテゴリー名を追加するのであれば「add_column_category_to_products_table –table=products」のようにします。

2. マイグレーションファイルにカラム追加の処理を記述
作成したマイグレーションファイルを開くと次のように書かれているはずです。
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddColumnCategoryToProductsTable extends Migration
{
public function up()
{
Schema::table('products', function (Blueprint $table) {
//ここにカラム追加の処理を記述
});
}
public function down()
{
Schema::table('products', function (Blueprint $table) {
//ここにカラム削除の処理を記述
});
}
}
このupメソッドに操作する処理(カラム追加)を書きます。そして、downメソッドにはロールバック処理(元に戻すための処理)を書きます。
実際にproductsにcategoryという項目を追加する場合は次のようになります。
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddColumnCategoryToProductsTable extends Migration
{
public function up()
{
Schema::table('products', function (Blueprint $table) {
$table->string('category')->after('name') //追加
});
}
public function down()
{
Schema::table('products', function (Blueprint $table) {
$table->dropColumn('category') //追加
});
}
}
afterはカラムの位置を指定するためのオプションで、上記の場合はnameカラムの次にcategoryカラムが追加されることになります。
3. マイグレーションの実行
マイグレーションファイルが完成したら、最後にカラム追加の処理を実行しましょう。
ターミナル上で以下のコマンド実行します。
php artisan migrate
これによりマイグレーションファイルのupメソッドが実行され、テーブルにカラムが追加されます。
もし、記述内容に誤りがあり、元に戻したい場合は、次のコマンドでファイルを実行します。
php artisan migrate:rollback
これにより、マイグレーションファイルのdownメソッドの処理が実行され、ロールバックできます。
おすすめのLaravelの書籍




