Add Comments to Correlate User Code with SQL Queries in Laravel
Published on by Paul Redmond
Laravel SQL Commenter is a package by Spatie to add comments to SQL queries made by Laravel. It uses sqlcommenter to augment SQL statements about the code:
/* typical query */select * from users /* comments added by this package */select * from "users"/*controller='UsersController',action='index'*/;
Once this package is installed, SQL comments get automatically included, and you can control what things are added to the comments and disable comments dynamically.
use Spatie\SqlCommenter\SqlCommenter; SqlCommenter::addComment('foo', 'bar'); // select * from "users"/*foo='bar'*/;
This package has the concept of a "commenter" class, which add useful information to queries, such as the controller and route information. Out of the box, this package includes the following commenters:
- ControllerCommenter
- RouteCommenter,
- JobCommenter
- FileCommenter
- CurrentUserCommenter
You can also add custom commenter classes using the provided Commenter
interface from this package.
You can learn about this package, get full installation instructions, and view the source code on GitHub. The author, Freek Van der Herten, wrote a post titled Add comments to SQL queries made by Laravel if you'd like to dive deeper into the background of this package.