LoginSignup
1
2

More than 5 years have passed since last update.

Laravel4 DBマイグレーションでのテーブル作成

Posted at

プロジェクトフォルダに移動

yamato@ubuntu:~/engineer-japan$ cd /home/yamato/engineer-japan/
yamato@ubuntu:~/engineer-japan$ ls
app  artisan  bootstrap  composer.json  composer.lock  CONTRIBUTING.md  phpunit.xml  public  readme.md  server.php  vendor

下記コマンド実行

php artisan migrate:make create_template_table

下記メッセージが表示される

Created Migration: 2015_05_05_195158_create_template_table
Generating optimized class loader
Compiling common classes
Compiling views

マイグレーションフォルダに移動

cd /home/yamato/engineer-japan/app/database/migrations/

マイグレーションファイルを編集

vi 2015_05_05_195158_create_template_table.php

デフォルトでは下記のようになっている。

<?php

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

class CreateTemplateTable extends Migration {

        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
                //
        }

        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
                //
        }

}

スキーマビルダーを作成。 company_basic_info というテーブルを作る。

Schema::create('company_basic_info', function($table)
{
    $table->increments('id');
    $table->string('name',255)->unique();
    $table->string('kana',255)->unique();
    $table->string('tel',40)->unique();
    $table->string('fax',40)->unique();
    $table->string('address',255)->unique();
    $table->string('url',255)->unique();
    $table->date('establishment');
    $table->integer('capital');
});

public function up() の中にスキーマビルダーを書き込む。

<?php

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

class CreateTemplateTable extends Migration {

        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {

                Schema::create('company_basic_info', function($table)
                {
                    $table->increments('id');
                    $table->string('name',255)->unique();
                    $table->string('kana',255)->unique();
                    $table->string('tel',40)->unique();
                    $table->string('fax',40)->unique();
                    $table->string('address',255)->unique();
                    $table->string('url',255)->unique();
                    $table->date('establishment');
                    $table->integer('capital');
                });

        }

        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
                //
        }

}

マイグレーションを実行する。

cd /home/yamato/engineer-japan/
php artisan migrate

下記が表示されるので、Y を入力する。

**************************************
*     Application In Production!     *
**************************************

Do you really wish to run this command?

下記が表示されたら成功。

Migration table created successfully.
Migrated: 2015_05_05_195158_create_template_table

DBに下記テーブルが作成されていることを確認する。

    mysql> desc company_basic_info;
    +---------------+------------------+------+-----+---------+----------------+
    | Field         | Type             | Null | Key | Default | Extra          |
    +---------------+------------------+------+-----+---------+----------------+
    | id            | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
    | name          | varchar(255)     | NO   | UNI | NULL    |                |
    | kana          | varchar(255)     | NO   | UNI | NULL    |                |
    | tel           | varchar(40)      | NO   | UNI | NULL    |                |
    | fax           | varchar(40)      | NO   | UNI | NULL    |                |
    | address       | varchar(255)     | NO   | UNI | NULL    |                |
    | url           | varchar(255)     | NO   | UNI | NULL    |                |
    | establishment | date             | NO   |     | NULL    |                |
    | capital       | int(11)          | NO   |     | NULL    |                |
    +---------------+------------------+------+-----+---------+----------------+
    9 rows in set (0.00 sec)
1
2
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
1
2