Laravel Cloud is here! Zero-config managed infrastructure for Laravel apps. Deploy now.

Using Database Comments to Track Columns With Sensitive Data

Last updated on by

Using Database Comments to Track Columns With Sensitive Data image

Recently, I was exploring a Rails application when I noticed that the developers had marked database columns as having sensitive data via a table's column definition. Various databases support comments for columns, but here's MySQL's explanation of a column comment:

A comment for a column can be specified with the COMMENT option, up to 1024 characters long. The comment is displayed by the SHOW CREATE TABLE and SHOW FULL COLUMNS statements. It is also shown in the COLUMN_COMMENT column of the Information Schema COLUMNS table.

In MySQL, we can add a comment to a column when creating or modifying a table using the following syntax:

CREATE TABLE `users` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'Example comment for name.',
`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
-- ...
)

In Laravel, we can easily add comments to table columns using the comment() method in a database migration on an existing or new column:

$table->string('email')
->unique()
->comment('The user\'s email address they use the log in');

You can inspect database column comments via the table's structure; here's an example of the migrations table in a fresh Laravel application:

Not all databases support table or column comments, but databases like MySQL and PostgreSQL allow you to add comments. If your database supports comments, they can be helpful descriptors of a column and used in various ways to help developers understand a schema.

Using Comments to Mark Columns with Sensitive Data

Comment usage could range from user-friendly comments about a column or other contextual information. But what if we used something like sensitive_data=true to mark fields with sensitive user or system information?

Even if your Laravel application is mature, you could create a migration to mark columns containing sensitive information to audit your data and help in removing sensitive information during exports, database cloning, or user information requests:

php artisan make:migration mark_columns_with_sensitive_data

Carefully defining the columns as they were, we can add a comment in the migration for any table and columns we want to mark as sensitive:

public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->string('email')->comment('sensitive_data=true')->change();
$table->string('name')->comment('sensitive_data=true')->change();
$table->string('password')->comment('sensitive_data=true')->change();
});
 
Schema::table('profiles', function (Blueprint $table) {
// ...
});
}

After running the migration, you will see that the database now has comments on the marked fields:

Finding all Tables and Columns Marked as Sensitive

We now have useful comments throughout our application that developers can use for context, but our new comments allow us to go beyond documentation! We can find all columns marked as sensitive across our application and use this information for privacy, database cloning, and more.

While each database is different, MySQL can query the information schema columns table to find all fields marked as sensitive. My sensitive_data=true is a convention, you can do whatever you want, but here's how you could find all columns across the database:

SELECT TABLE_NAME, COLUMN_NAME, COLUMN_COMMENT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_COMMENT LIKE '%sensitive_data=true%';

Here's the output from our above example:

Taking it Further

What if we wanted an easy way to get all the sensitive column names for a given model? There are probably various ways to achieve this, but what if we built an application trait our models could use to find sensitive columns:

namespace App\Models\Concerns;
 
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
 
/**
* @mixin \Illuminate\Database\Eloquent\Model
*/
trait InteractWithSensitiveColumns
{
/**
* @return Collection<string>
*/
public function getSensitiveColumns(): Collection
{
return $this->getConnection()
->table('INFORMATION_SCHEMA.COLUMNS')
->where('TABLE_NAME', $this->getTable())
->where('COLUMN_COMMENT', 'like', '%sensitive_data=true%')
->pluck('COLUMN_NAME');
}
}

Let's take this trait for a spin in a User model instance, and see how it works:

Note that using the above trait locks you into using MySQL for models using this method. I am unsure if there is a database-agnostic way to get this information, but be aware that the above trait is MySQL-specific.

Learn More

I hope you've seen a few ways that database comments could be useful for maintaining your application database! Whether you are creating a new application or working on an existing app, you can start using comments right away! You can learn more about managing your database through Laravel's migrations and Eloquent models in the Database documentation.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Bacancy

Outsource a dedicated Laravel developer for $3,200/month. With over a decade of experience in Laravel development, we deliver fast, high-quality, and cost-effective solutions at affordable rates.

Visit Bacancy
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
Factory makeMany() Method in Laravel 12.52.0 image

Factory makeMany() Method in Laravel 12.52.0

Read article
Laravel Adds an Official Svelte + Inertia Starter Kit image

Laravel Adds an Official Svelte + Inertia Starter Kit

Read article
MongoDB Vector Search in Laravel: Finding the Unqueryable image

MongoDB Vector Search in Laravel: Finding the Unqueryable

Read article
Laravel Cloud Adds “Markdown for Agents” to Serve AI-Friendly Content image

Laravel Cloud Adds “Markdown for Agents” to Serve AI-Friendly Content

Read article
Laravel Releases Nightwatch MCP Server for Claude Code and AI Agents image

Laravel Releases Nightwatch MCP Server for Claude Code and AI Agents

Read article
Single Table Inheritance for Eloquent Models Using Parental image

Single Table Inheritance for Eloquent Models Using Parental

Read article