Laravel Schema Builder,设置字段描述

问题描述 投票:0回答:3

是否可以使用 laravel 的架构生成器向 sql 字段添加描述/注释。就像在 drupal 中一样?

laravel
3个回答
5
投票

原来可以添加评论,但是好像没有记录。 这篇 Laracasts 帖子展示了如何通过在行尾添加“comment”属性来实现。

以他们的例子,

 Schema::create('products', function(Blueprint $table)
 {
    $table->increments('id');
    $table->string('product_name')->comment = "Product name column";
    $table->timestamps();
  });
 }

事实证明 - 现在只是测试 - 您实际上可以使用更典型的函数语法,例如,

$table->string('product_name')->comment('Product name column');

...类似于设置

->default(...)
->nullable()
。有些人可能更喜欢这种风格以保持一致性。

从 Laravel 5 开始,使用 MySQL 似乎效果很好。这可能是最近的改进。


0
投票

模式构建器不支持描述/注释,将来可能也不支持。你必须退回到 SQL:

假设你使用MySQL

Schema::create('users', function(Blueprint $table){
    $table->increments();
    $table->text('username');
    $table->text('password', 60);
});

DB::statement('ALTER TABLE `users` CHANGE `password` `password` VARCHAR(60) COMMENT 'password hash');

0
投票

对于仍在寻找此内容的任何人,可以在每个文档的 Laravel 5.2 中向表列添加注释。

这是 Laravel 11.x 的链接

示例:

Schema::table('some_table', function (Blueprint $table) {
   $table->string('some_column')->comment('This is a description/comment for some column')->nullable();
});
© www.soinside.com 2019 - 2024. All rights reserved.