LoginSignup
24
24

More than 5 years have passed since last update.

マイグレーションまとめ(Laravel5)

Last updated at Posted at 2017-02-21

Laravelではデフォルトでusersテーブルのマイグレーションがある。

マイグレーションを作る

1) artisanコマンドを実行「php artisan make:migration XXXXX」
database/migrations/に、yyyy_mm_dd_hhmmss_XXXXX が作成される
2) マイグレーションファイルに、up downを記述 (カラムを追加する)
3) マイグレーション実行「$php artisan migration」

[オプション]
--createオプション テーブルの作成  Schema::create()
--tableオプション  テーブルの編集 Schema::table()

マイグレーションのサンプル1

$ php artisan make:migration create_posts_table --create=posts 

--createオプションでテーブル名を指定すると新規でテーブル作成。

ファイル名
2017_02_10_114629_create_posts_table.php

クラス名
class CreatePostsTable extends Migration

2017_02_10_114629_create_posts_table.php


<?php

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

//マイグレーション名をキャメルケースに変換したものがクラス名になる
class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
    //オプションで渡した名前がテーブル名になる
    //--createオプション Schema::createになる
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('body');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('posts');
    }
}

マイグレーションのサンプル2(※外部キー制約参照)

マイグレーションとモデルを同時に作る場合

$ php artisan make:model Comment --migration

ファイル名
2017_02_13_110711_create_comments_table.php

クラス名
class CreateCommentsTable extends Migration


<?php

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

//マイグレーション名をキャメルケースに変換したものがクラス名になる
class CreateCommentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');
            //負の値になることは無いので、unsigned()
            $table->integer('post_id')->unsigned();
                        //一行コメントなので string('body')
            $table->string('body');
            $table->timestamps();

                        //外部キー制約 postsテーブルのidと紐付け
            $table->foreign('post_id')
                  ->references('id')
                  ->on('posts')
                  ->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('comments');
    }
}

外部キー制約

onDelete

  • CASCADE  親テーブルに対して更新を行うと子テーブルで同じ値を持つカラムの値も合わせて更新される

  • SET NULL 親テーブルに対して更新/削除を行うと子テーブルで同じ値を持つカラムの値がNULLに更新される

  • RESTRICT エラーが発生

  • NO ACTION RESTRICTと同じ

モデル サンプル

app/Post.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    //
    protected $fillable = ['title', 'body'];

    public function comments() {
      return $this->hasMany('App\Comment');
    }
}
app/Comment.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    //
    protected $fillable = ['body'];

    // comment->post
    public function post() {
      return $this->belongsTo('App\Post');
    }
}
24
24
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
24
24