0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

OctoberCMS Migrate

Posted at

#OctoberCMSでテーブルを作成するには
plugin > プラグイン名 > プロジェクト名 > updates 以下にファイルを作成する。

##テーブル作成
例)usersテーブルを作成する

create_users_table.php
<?php namespace Kobayashi\Corporate\Updates;

use Schema;
use October\Rain\Database\Schema\Blueprint;
use October\Rain\Database\Updates\Migration;

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('プラグイン名_プロジェクト名_users', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->string('fullname');
            $table->string('email');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('kobayashi_corporate_estimates');
    }
}

##version.yaml作成
updatesファイルの中に作成

version.yaml
1.0.1: First version of プロジェクト名
1.0.2:
  - 'create table users'     //コメント
  - create_users_table.php    //実行するファイル名

##カラム追加

update_users_table.php

<?php namespace プラグイン名\プロジェクト名\Updates;

use Schema;
use October\Rain\Database\Schema\Blueprint;
use October\Rain\Database\Updates\Migration;

class UpdateUsersTable extends Migration
{
    public function up()
    {
        Schema::table('プラグイン名_プロジェクト名_users', function (Blueprint $table) {
            $table->string('tel')->nullable();
     });
    }
    public function down()
    {
        Schema::table('プラグイン名_プロジェクト名_users', function ($table) {
            $table->dropColumn('tel');
        });
    }

}

###version.yaml追記

version.yaml
1.0.1: First version of プロジェクト名
1.0.2:
  - 'create table users'    
  - create_users_table.php
- 1.0.3:
- 'update table users'
- update_users_table.php

##カラム変更

update_users_table_2.php
<?php namespace プラグイン名\プロジェクト名\Updates;

use Schema;
use October\Rain\Database\Schema\Blueprint;
use October\Rain\Database\Updates\Migration;

class UpdateUsersTable2 extends Migration
{
    public function up()
    {
        Schema::table('プラグイン名_プロジェクト名_users', function ($table) {
            $table->dropColumn('email');
        });
     
    }
    public function down()
    {
    Schema::table('プラグイン名_プロジェクト名_users', function (Blueprint $table) {
            $table->string('email')->nullable();
     });
    }

}
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?