日期: 2022年5月24日

  • 在 Laravle 6 的数据库迁移中添加表注释

    在 Laravle 6 的数据库迁移中添加表注释

    1、在 Laravle 6 的数据库迁移中,数据表选项不支持添加注释。如图1
    在 Laravle 6 的数据库迁移中,数据表选项不支持添加注释
    图1
    2、参考:https://stackoverflow.com/questions/37493431/how-to-add-comment-to-table-not-column-in-laravel-5-migration 。How to add comment to table (not column) in Laravel 5 migration? 如图2
    参考:https://stackoverflow.com/questions/37493431/how-to-add-comment-to-table-not-column-in-laravel-5-migration 。How to add comment to table (not column) in Laravel 5 migration?
    图2
    3、最终实现如下
    <pre class="wp-block-syntaxhighlighter-code">
    
    <?php
    
    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class CreateThemeInstallationTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            $tableName = 'theme_installation';
    
            Schema::create($tableName, function (Blueprint $table) {
                $table->increments('id');
                // ...
                $table->timestamps();
            });
    
            DB::statement('ALTER TABLE ' . DB::connection()->getTablePrefix() . $tableName . ' COMMENT \'主题安装\'');
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('theme_installation');
        }
    }
    
    
    </pre>
    
    4、执行数据库迁移后,查看执行结果,表注释已经实现。如图3
    执行数据库迁移后,查看执行结果,表注释已经实现
    图3