spatie/laravel-sql-commenter
Adds sqlcommenter-style comments to Laravel database queries, embedding context like controller and action. Makes it easy to trace slow or problematic SQL back to the exact code path, and works with tools like PlanetScale Query Insights.
Installation:
composer require spatie/laravel-sql-commenter
Publish the config file (optional):
php artisan vendor:publish --provider="Spatie\SqlCommenter\SqlCommenterServiceProvider"
First Use Case:
Enable the package in config/sql-commenter.php:
'enabled' => env('SQL_COMMENTER_ENABLED', false),
Set SQL_COMMENTER_ENABLED=true in your .env and restart your app.
Verify: Run a query (e.g., User::all()) and check your database logs or PlanetScale Query Insights for comments like:
select * from "users"/*controller='App\Http\Controllers\UserController',action='index'*/
Automatic Commenting:
The package hooks into Laravel’s query builder (DB::query(), Eloquent, Query Builder) and appends comments in the format:
/*controller='{Controller}',action='{Method}',model='{Model}'*/
UserController@show query becomes:
select * from "users" where "id" = ?/*controller='UserController',action='show',model='App\Models\User'*/
Customizing Comments:
Override the default comment format via config/sql-commenter.php:
'comment_format' => '/*source={controller}.{action},model={model},user={user_id}*/',
{controller}, {action}, {model}, {user_id} (requires middleware).Conditional Commenting: Disable for specific queries:
DB::connection()->disableCommenting();
User::where('active', true)->get(); // No comment added
Middleware Integration: Attach user IDs to comments via middleware:
use Spatie\SqlCommenter\Middleware\AddUserIdToSqlComments;
protected $middleware = [
AddUserIdToSqlComments::class,
];
laravel-debugbar or monolog to log commented queries for debugging.$this->app->make('Spatie\SqlCommenter\SqlCommenter')->shouldReceive('commentQuery')->andReturn('/*test*/');
Performance Overhead:
'enabled' => app()->environment('production') ? false : true,
Query Builder vs. Raw SQL:
DB::select("SELECT ...")) won’t be commented unless wrapped in a builder:
DB::select(DB::raw("SELECT * FROM users")); // Commented
DB::select("SELECT * FROM users"); // Not commented
Anonymous Controllers:
Route::get('/', fn() => ...)) may show as controller='__lambda'. Rename via middleware:
use Spatie\SqlCommenter\Middleware\SetLambdaControllerName;
protected $middleware = [SetLambdaControllerName::class];
Nested Transactions:
DB::beginTransaction();
DB::connection()->disableCommenting();
// ...
DB::commit();
Missing Comments:
config/sql-commenter.php for enabled: false.Spatie\SqlCommenter\SqlCommenterServiceProvider is registered in config/app.php.Incorrect Formatting:
php artisan config:clear
comment_format placeholders.Custom Comment Logic: Extend the commenter via a binding:
$this->app->bind('Spatie\SqlCommenter\Contracts\Commenter', function () {
return new CustomCommenter();
});
Dynamic Comment Data: Add custom metadata via events:
use Spatie\SqlCommenter\Events\CommentingQuery;
CommentingQuery::listen(function ($event) {
$event->commentData['custom_key'] = 'custom_value';
});
Database-Specific Quirks:
/* */ syntax (default) or adjust comment_format to use -- for line comments if needed.How can I help you explore Laravel packages today?