Blog Detail

15

Jul
A Package to Add Comments to SQL Queries Made by Laravel cover image

arrow_back A Package to Add Comments to SQL Queries Made by Laravel

Laravel SQL Commenter package can add comments to queries performed by Laravel. These comments will use the sqlcommenter format, which is understood by various tools and services, such as PlanetScale’s Query Insights.

Installation

You can install the package via composer:

composer require spatie/laravel-sql-commenter

Optionally, you can publish the config file with:

php artisan vendor:publish --tag="sql-commenter-config"

Usage

With the package installed, comments are automatically added. By publishing the config file, you can choose which things are added to the comments.

Adding arbitrary comments

If you want to add other arbitrary comments to the SqlComment, you can use the addComment method. The given comment will be added to the next performed query.

use Spatie\SqlCommenter\SqlCommenter;

SqlCommenter::addComment('foo', 'bar');

// select * from "users"/*foo='bar'*/;

Dynamically enabling and disabling adding comments

You can dynamically enable and disable query logging.

Let’s assume that you only want to add comments for a certain part in your code base. First, you would need to set the value of the enabled key in the sql-commenter config file to false. This will stop the package from adding comments to all queries. Right before the part where you want to add comments, call SqlCommenter::enable() and at the end call SqlCommenter::disable()

use \Spatie\SqlCommenter\SqlCommenter;

// queries performed here won't have comments

SqlCommenter::enable();

// queries performed here will have comments

SqlCommenter::disable();

// queries performed here won't have comments

Adding your own commentator

If you want to add a comment to all performed queries, you can create your own Commentator class. It should implement the Spatie\SqlCommenter\Commenters\Commenter interface. The comments function should return a single or an array of Spatie\SqlCommenter\Comment.

Here’s an example:

namespace App\Support\SqlCommenters;

use Illuminate\Database\Connection;
use Spatie\SqlCommenter\Comment;

class MyCustomCommenter implements Commenter
{
    /** @return Comment|array<Comment>|null */
    public function comments(string $query, Connection $connection): Comment|array|null
    {
        return new Comment('my-custom-key',  'my-custom-value');
    }
}

For more details, Visit Github

Closing Notes

If you are an enterprise and want to use develop a CRM, e-commerce, or SAAS web application in Laravel, you must discover an impeccable team with proficiency in the Laravel framework. So Codebrisk is here to help you with your tailored requirements regarding Laravel Development. Our CRM development with Laravel holds a wide range of features for entities to manage their interactions with customers

So if you have a great idea, then please feel free to contact us or you can launch a project with us here.

Published at : 15-07-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project