Help us understand the problem. What is going on with this article?

LaravelでPostgreSQLのmigrateにてコメントが登録されない

More than 3 years have passed since last update.

概要

PostgreSQLを使ってLaravelで開発しているとcomment()メソッドで指定しているのにテーブルにコメントが登録されていないことに気づき調べてみたことをメモしておく。

調べた結果

下記記事にも書かれているが、どうやらPostgreSQLの場合使えるメソッドが少ないようですね。
comment()メソッドについては、エラーにもならないので気づかなかったですが、どうやら非対応みたい...なんと(驚)

Laravel のマイグレーションでテーブルカラムにコメントを設定する
https://teratail.com/questions/91732

そこで、対応していないのであれば自分で作るしかないですね。

CustomBlueprintクラス作成

app/Migrates/CustomBlueprint.php
<?php

namespace App\Migrates;

use Illuminate\Database\Schema\Blueprint;

/**
 * Blueprintクラスの拡張
 *
 * @package App\Migrate
 */
class CustomBlueprint extends Blueprint
{
    /**
     * テーブルにコメントを追加する
     *
     * @param  string   $message
     * @return \Illuminate\Support\Fluent
     */
    public function commentTable($message)
    {
        return $this->addCommand(
            'commentTable', compact('message')
        );
    }

    /**
     * カラムにコメントを追加する
     *
     * @param  string   $column
     * @param  string   $message
     * @return \Illuminate\Support\Fluent
     */
    public function commentColumn($column, $message)
    {
        return $this->addCommand(
            'commentColumn', compact('column', 'message')
        );
    }
}

CustomPgsqlGrammarクラス作成

app/migrates/CustomPgsqlGrammar.php
<?php

namespace App\Migrates;

use Illuminate\Support\Fluent;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Grammars\PostgresGrammar;

/**
 * PostgresGrammarクラスを拡張
 *
 * @package App\Migrate
 */
class CustomPgsqlGrammar extends PostgresGrammar
{
    /**
     * コメントをテーブルに追加する
     *
     * @param  \Illuminate\Database\Schema\Blueprint  $blueprint
     * @param  \Illuminate\Support\Fluent  $command
     * @return string|null
     */
    public function compileCommentTable(Blueprint $blueprint, Fluent $command)
    {
        return sprintf('comment on table %s is %s',
            $this->wrapTable($blueprint),
            "'".$command->message."'"
        );
    }

    /**
     * コメントをカラムに追加する
     *
     * @param  \Illuminate\Database\Schema\Blueprint  $blueprint
     * @param  \Illuminate\Support\Fluent  $command
     * @return string|null
     */
    public function compileCommentColumn(Blueprint $blueprint, Fluent $command)
    {
        return sprintf('comment on column %s.%s is %s',
            $this->wrapTable($blueprint),
            $this->wrap($command->column),
            "'".$command->message."'"
        );
    }
}

使ってみる

database/migrations/****_**_**_******_create_users_table.php
<?php

use Illuminate\Support\Facades\Schema,
    Illuminate\Database\Schema\Blueprint,
    Illuminate\Database\Migrations\Migration,
    App\Migrates\CustomBlueprint,
    App\Migrates\CustomPgsqlGrammar;

/**
 * 会員テーブルの作成
 */
class CreateUsersTable extends Migration
{
    /**
     * マイグレーション実行
     *
     * @access  public
     * @return  void
     */
    public function up()
    {
        DB::connection()->setSchemaGrammar(new CustomPgsqlGrammar());
        $schema = DB::connection()->getSchemaBuilder();

        $schema->blueprintResolver(function($table, $callback) {
            return new CustomBlueprint($table, $callback);
        });

        $schema->create('users', function (CustomBlueprint $table) {
            $table->bigincrements('id');
            $table->string('email');
            $table->password('password');
            $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
            $table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP'));
            $table->softDeletes();

            // コメント定義
            $table->commentTable('会員');
            $table->commentColumn('email', 'メールアドレス');
            $table->commentColumn('password', 'パスワード');
            $table->commentColumn('created_at', '作成日時');
            $table->commentColumn('updated_at', '更新日時');
            $table->commentColumn('deleted_at', '削除日時');
        });
    }

    /**
     * 元に戻す
     *
     * @access  public
     * @return  void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

参考サイト

reflet
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした