読者です 読者をやめる 読者になる 読者になる

わすれっぽいきみえ

みらいのじぶんにやさしくしてやる

21日目: Laravel Eloquentを使って必要なModelを生成する その2

laravel.com

ここにあるコマンドたちで適当にmigrationファイルをいじる。

reviewsテーブルの作成。

<?php

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

class CreateReviewsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('reviews', function (Blueprint $table) {
            $table->increments('id');
            $table->string('hosting_type')->default('github');
            $table->string('repository_url');
            $table->unsignedMediumInteger('number');
            $table->string('title');
            $table->unsignedInteger('author_id');
            $table->unsignedTinyInteger('status')->default(0);
            $table->timestamps();

            $table->unique(['hosting_type', 'repository_url', 'number']);
            // $table->foreign('author_id')->references('id')->on('users');
        });
    }

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

commentsテーブルの作成。

<?php

use Illuminate\Support\Facades\Schema;
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');
            $table->string('comment_type');
            $table->unsignedInteger('review_id');
            $table->unsignedInteger('commented_user_id');
            $table->string('file_name');
            $table->unsignedInteger('line');
            $table->text('content');
            $table->timestamps();
        });
    }

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

usersテーブルも作成したいが、すでにファイルがあったので差分を書くと以下。

         Schema::create('users', function (Blueprint $table) {
             $table->increments('id');
             $table->string('name');
-            $table->string('email')->unique();
-            $table->string('password');
-            $table->rememberToken();
+            $table->string('email');
+            $table->string('url');
+            // $table->string('password');
+            // $table->rememberToken();
             $table->timestamps();
         });
     }

上記ファイルを作った上で、migrationを実行すると

$ php artisan migrate
Migrated: 2016_12_18_134444_create_reviews_table
Migrated: 2016_12_18_134451_create_comments_table

む、usersテーブルもすでにあったか?

MariaDB [kamattechan]> show create table reviews\G
*************************** 1. row ***************************
       Table: reviews
Create Table: CREATE TABLE `reviews` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `hosting_type` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'github',
  `repository_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `number` mediumint(8) unsigned NOT NULL,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `author_id` int(10) unsigned NOT NULL,
  `status` tinyint(3) unsigned NOT NULL DEFAULT '0',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `reviews_hosting_type_repository_url_number_unique` (`hosting_type`,`repository_url`,`number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
1 row in set (0.00 sec)

MariaDB [kamattechan]> show create table comments\G
*************************** 1. row ***************************
       Table: comments
Create Table: CREATE TABLE `comments` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `comment_type` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `review_id` int(10) unsigned NOT NULL,
  `commented_user_id` int(10) unsigned NOT NULL,
  `file_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `line` int(10) unsigned NOT NULL,
  `content` text COLLATE utf8_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
1 row in set (0.00 sec)

MariaDB [kamattechan]> show create table users\G
*************************** 1. row ***************************
       Table: users
Create Table: CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
1 row in set (0.00 sec)

おお password カラムなくなってるし、とりあえずこれで良さそう。

ちなみに全テーブルをもう一回作り直すとか男前なことをしたい場合は以下のやり方でいける。

$ php artisan migrate:refresh --seed
Rolled back: 2016_12_18_134451_create_comments_table
Rolled back: 2016_12_18_134444_create_reviews_table
Rolled back: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2016_12_18_134444_create_reviews_table
Migrated: 2016_12_18_134451_create_comments_table

Eloquentまだいけないな…。今日はここまで。