Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ipunkt's avatar
Level 13

How to set a comment on table column using Schema Builder

I'd like to add comments on some table columns so I can re-think or communicate to other developers what is the concrete meaning on some unconcrete column names. In some big and long-living applications you can not modify the database schema so easy when modernizing only one part of the whole app. A comment is a suitable way I think.

0 likes
9 replies
bashy's avatar

Just put that info in the migration? That's what they're acting as anyway.

ipunkt's avatar
Level 13

But I want to set it on the tables column.

xingfucoder's avatar
Level 14

Hi @ipunkt, you could use the following comment property:

class CreateProductsTable extends Migration {

 /**
 * Run the migrations.
 *
 * @return void
 */
 public function up()
 {
 Schema::create('products', function(Blueprint $table)
 {
    $table->increments('id');
    $table->string('product_name')->comment = "Product name column";
    $table->timestamps();
  });
 }


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

}

It belongs to the MySQLGrammar class that you can find here:

<?php namespace Illuminate\Database\Schema\Grammars;

use Illuminate\Support\Fluent;
use Illuminate\Database\Connection;
use Illuminate\Database\Schema\Blueprint;

class MySqlGrammar extends Grammar {

 /**
  * The possible column modifiers.
  *
  * @var array
  */
 protected $modifiers = array('Unsigned', 'Nullable', 'Default', 'Increment', 'After', 'Comment');

https://github.com/illuminate/database/blob/9a9149e6b8c1e9af29a6bba92779a2a501034cae/Schema/Grammars/MySqlGrammar.php

I think you may use within table modifications as follow:

Schema::table('users', function($table)
 {
    $table->renameColumn('from', 'to')->comment = "New Comment of Product name column";
 });

Hope it helps you.

4 likes
xingfucoder's avatar

You are welcome @ipunkt, I discovered searching when you requested it. So, this helps me too.

1 like
teamvoto's avatar

Thanks @codeatbusiness ! Really helped, I think you're the first one to document this. :)

It turns out that the usual "function" syntax also works, at least on L5 (tested today!) Some people might prefer that for consistency, or so that you can put the comment anywhere in the chain of functions:

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

That's really nice! There's an old Github post from back before this was implemented with a lot of requests for it, so it's cool to see that it has made it in. Thanks for pointing us all in the right direction!

4 likes
xingfucoder's avatar

You are welcome @teamvoto, is great we may use with the comment function.

Thanks for sharing that tip.

1 like

Please or to participate in this conversation.