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

devondahon's avatar

How to retrieve column comments ?

I'm setting column comments like below:

$table->string('gender', 1)->nullable()->comment("m: male\nf: female");

How to retrieve it with artisan tinker ?

0 likes
5 replies
Tray2's avatar

Why would you want to do that?

What you could do is create a table called genders.

gender   | description
--------------------------------
m        | male
f        | female
o        | other

Then use the gender as the primary and foreign key in your table if you want a description of the abbriviation.

devondahon's avatar

@tray2 What's the point of allowing to comment a table column if there's no way to re-use this information ?

Which PostGreSQL, it's possible to retrieve comments this way:

SELECT
    c.table_schema,
    st.relname AS TableName,
    c.column_name, 
    pgd.description
FROM pg_catalog.pg_statio_all_tables AS st
INNER JOIN information_schema.columns c
ON c.table_schema = st.schemaname
AND c.table_name = st.relname
LEFT join pg_catalog.pg_description pgd
ON pgd.objoid=st.relid
AND pgd.objsubid=c.ordinal_position
WHERE st.relname = 'YourTableName';
Tray2's avatar

Yes you can retreive it but I wouldn't use comments in my application like that.

You can always just do a regular select on it.

devondahon's avatar

@tray2 How do you do a regular select on it ? And how do you get these comments with artisan tinker ?

Tray2's avatar

You can do something like this

\DB::select("SELECT COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE  TABLE_SCHEMA = '<your database>' AND TABLE_NAME = '<your table>' AND COLUMN_NAME = 'your column'");

Please or to participate in this conversation.