13

How to add comment to table (ot column) in Laravel 5 migration?

I currently know how to add comment to column like:

$table->tinyInteger('status')->comment('0: requested; -1: rejected; 1:confirmed');

But what about table?

mohsenJsh
  • 2,048
  • 3
  • 25
  • 49
  • 2
    Possible duplicate of [How to set a comment on table using Laravel Schema Builder](http://stackoverflow.com/questions/34271843/how-to-set-a-comment-on-table-using-laravel-schema-builder) – num8er Jul 21 '16 at 08:44
  • Better and working answer [here](https://stackoverflow.com/a/34272884/1883256), like "num8er" commented. – Pathros May 13 '21 at 17:00

3 Answers3

19

Currently, Laravel does not allow (does not have functionality) to put comment on tables, so You have to use workaround in Your migration:

DB::statement("ALTER TABLE `<YOUR_TABLE>` comment '<COMMENT>'");
Giedrius Kiršys
  • 5,154
  • 2
  • 20
  • 28
  • 1
    you can make some static helper function e.g. setTableComment($table, $comment) which wrap above code (to not use it directly in migration files, if some db not support above syntax) – Kamil Kiełczewski Dec 06 '17 at 15:02
  • @KamilKiełczewski Can you include an example of what such a function looks like and where it goes? Thanks. – Jay Bienvenu Sep 20 '19 at 16:14
1

For now there are no any option exist to add table comment like column add but then after you want to add the comment to table than you must have to use "DB" to add the comment to table.

For Example,

If you want to add the comment to website table then through below Syntax you can add the comment to table.

DB::select("ALTER TABLE website COMMENT = 'This table contains the website information for the application'");

NOTE: Before use above line you must have migrated website/ your table then n than you can use this.

Chirag Viradiya
  • 477
  • 4
  • 10
0

No need to use DB::statement() and RAW SQL queries!

Just add the comment() chain method on $table variable:

$table->comment('your comment');
mjavadtatari
  • 51
  • 2
  • 9