当还在用 laravel 8.x 的时候,在 migration 数据迁移文件中,给 mysql 表添加注释,需要增加一条 DB::statment( ) 语句:

class CreateTestTable extends Migration
{

    public function up()
    {
        Schema::create('test', function (Blueprint $table) {
            $table->id()->comment('字段注释');
            //...
        });

        DB::statement("alter table `test` comment '表注释'");
    }

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

 

当你升级到 laravel 9.14.0 版本后,就不需要这样做了,可使用 $table->comment( ) 语句,如:

return new class extends Migration
{

    public function up()
    {
        Schema::create('test', function (Blueprint $table) {
            $table->id()->comment('字段注释');
            //...

            $table->comment('表注释');
        });
    }

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