Adds a SQLCommenter comment to every statement
Laravel's database layer sends, naming the file, line and method in your code that issued it,
and the route and HTTP method of the request being handled. That covers Eloquent models, the
query builder and raw DB::select calls alike.
Laravel discovers the service provider on its own, so installing the package is the whole setup. A query issued at line 42 of
/srv/app/app/Http/Controllers/OrderController.php, inside index, while handling
GET /customers/7/orders on the route customers/{customer}/orders, reaches the database as:
select * from "orders" where "customer_id" = $1/*db_driver='eloquent',file='%2Fsrv%2Fapp%2Fapp%2FHttp%2FControllers%2FOrderController.php%3A42',func_name='OrderController.index',method='GET',route='%2Fcustomers%2F%7Bcustomer%7D%2Forders'*/The values are encoded the way the other packages in this repository encode them, so a reader of one reads the other.
file: absolute path and line of the first stack frame that is your code. Frames undervendor/and from this package are skipped, and so arepublic/index.phpandartisan: a query the framework issues itself, such as a session lookup, gets nofiletag. PHP has no column.func_name:ShortClassName.method, or the function's name. A closure, such as the callback given toDB::transaction(), is named after the method or function it is written in. Omitted at the top of a script and for a closure written there. Before PHP 8.4, a closure called after its function returned is not named either when that function is a plain function, or when the closure was bound to another class.route: the pattern of the route handling the request, as Laravel declares it, with a leading/:/users/{user}, never/users/42.method: the request's HTTP method.db_driver: alwayseloquent.
route and method are read from the request the router matched when the statement is sent.
A statement sent outside a matched route carries neither: an artisan command, a queue worker,
a request no route matched, or a script using illuminate/database alone. Laravel keeps the last request after
handling it, so in a feature test a statement sent after $this->get(...) still names that
request's route.
A lazy load is tagged with the line that touched the relation, so an N+1 names the loop that
causes it. A statement that already ends in a comment is sent unchanged, and a trailing ; stays
last.
Only the database sees the comment. Laravel's query log, DB::listen listeners and query
exceptions keep the statement as your code wrote it.
The service provider registers a connection resolver for each driver Laravel ships (pgsql,
mysql, mariadb, sqlite, sqlsrv). Each resolver builds Laravel's own connection class
with the TagsStatements trait added. A driver your app already registered a resolver for keeps
yours; add the trait to that connection class to get the tags:
use QueryDoctor\SqlCommenter\Eloquent\TagsStatements;
class MyPostgresConnection extends \Illuminate\Database\PostgresConnection
{
use TagsStatements;
}Outside Laravel, with illuminate/database alone, call
QueryDoctor\SqlCommenter\Eloquent\SqlCommenter::register() before the first connection opens.
PHP 8.2 or later. Tested with Laravel 12 and 13 against Postgres 17, in a Laravel app booted by Orchestra Testbench.
composer install
vendor/bin/phpunitThe end-to-end tests need a Postgres with pg_stat_statements loaded, named in
SQLCOMMENTER_PG as user:password@host:port/db; without it they are skipped.
This package is developed in Query-Doctor/sqlcommenter, under php/sqlcommenter-eloquent. The standalone repository Packagist reads is a copy that a workflow updates on every merge, so send issues and pull requests to the monorepo. A release is a tag php/sqlcommenter-eloquent/vX.Y.Z on master, which the workflow turns into the tag vX.Y.Z here.