目次
新規にテーブルを作成したい場合
以下のコマンドで新規にテーブルを作成するためのマイグレーションファイルが生成します。
$ php artisan make:migration create_articles_table
create_articles_tableの部分はマイグレーションファイルの名前になるだけなので、自分がわかればなんでもいいです。
生成したファイルの中身は以下のようになっています。
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateArticlesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
}
中身の確認
簡単にですが、中身を確認していきます。
up関数は、$php artisan migrateで実行され、
down関数は、$php artisan migrate:rollbackで実行されます。
Schema::create(“テーブル名”, 〜)
Schemaクラスのcreateメソッドでテーブルを作成することができます。
Schema::dropIfExists(“テーブル名”)
Schema::dropIfExistsはファイル作成時に、down関数の中に記述されており、ロールバックされた時に実行されます。
dropIfExistsメソッドは、引数で指定したテーブルが存在していれば、削除します。
既存のテーブルを変更したい場合
$ php artisan make:migration change_articles_table –table=articles
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ChangeArticlesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('articles', function (Blueprint $table) {
//
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('articles', function (Blueprint $table) {
//
});
}
}
中身の確認
Schema::table(“テーブル名”, 〜)
今回は、up関数にもdown関数にも、tableメソッドが指定されています。
データ型一覧
・bigIncrements(‘id’)
・increments(‘id’)
・string(‘カラム名’)
・text(‘カラム名’)
・integer(‘カラム名’)
・tinyInteger(‘カラム名’)
・bigInteger(‘カラム名’)
・date(‘カラム名’)
・timestamps()
・softDeletes()
オプション一覧
・nullable()
・unique()
・default()
・unsigned()
・comment(‘コメント内容’)
・index(‘カラム名’)
動かしてみる:カラムの作成・外部キー設定・変更
$ php artisan make:migration change_articles_table –table=articles
では、articlesテーブルを例に、作成・usersテーブルの外部キー設定・変更を紹介します。
作成
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title'); // 追加
$table->string('text'); // 追加
$table->string('category'); // 追加
$table->timestamps();
});
}
titleカラムと記事の内容を格納するtextカラムを追加しました。
外部キー制約の設定
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('text');
$table->string('category');
$table->integer('user_id')->unsigned(); // 追加
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); // 追加
$table->timestamps();
});
}
$table->integer(‘user_id’)の部分で、user_idカラムを生成しています。
unsigned()では、integer型の整数に対して符号をつけない、を指定しています。
次に、$table->foreign(‘user_id’)->references(‘id’)->on(‘users’)の部分で、usersテーブルのidカラムを参照してuser_idカラムに外部キー制約を追加、を表しています。
onDelete(‘cascade’)の部分は、親テーブルusersが削除されたら同時に削除する、を表しています。
変更
画像も投稿できるように、先ほど作成したarticlesテーブルに、
・imageカラムにNULL許可設定をする
・textカラム名をcontentカラム名に変更する
・categoryカラムを削除する
の変更を加えていきます。
まずは、以下のコマンドで、マイグレーションファイルを作成します。
public function up()
{
Schema::table('articles', function (Blueprint $table) {
$table->string('image')->nullable()->change(); // 追加
$table->string('text', 'content') // 追加
$table->dropColumn('category'); // 追加
});
}
nullable()などのオプションを追加する場合は、changeメソッドを用います。
また、カラムを削除したい場合は、
dropColumnメソッドを用います。
参考記事
必要最低限を知りたい → Laravel学習帳
もっと詳しく知りたい → Laravel 5.5 データベース