PHPackages                             cybex/laravel-query-tracer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. cybex/laravel-query-tracer

ActiveLibrary[Database &amp; ORM](/categories/database)

cybex/laravel-query-tracer
==========================

Add stacktraces to your Model queries.

1.0.1(4y ago)1375↓33.3%[1 issues](https://github.com/cybex-gmbh/laravel-query-tracer/issues)MITPHPPHP ^7.2.5|^8.0

Since Oct 22Pushed 4y ago1 watchersCompare

[ Source](https://github.com/cybex-gmbh/laravel-query-tracer)[ Packagist](https://packagist.org/packages/cybex/laravel-query-tracer)[ Docs](https://github.com/cybex-gmbh/laravel-query-tracer)[ RSS](/packages/cybex-laravel-query-tracer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (5)Versions (11)Used By (0)

Laravel Query Tracer
====================

[](#laravel-query-tracer)

[![Latest Version on Packagist](https://camo.githubusercontent.com/1e7a7448e4c45252d107115d2335230627f12f424c729b81dcf8fe226e246afc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f63796265782f6c61726176656c2d71756572792d7472616365722e737667)](https://packagist.org/packages/cybex/laravel-query-tracer)[![Packagist Downloads](https://camo.githubusercontent.com/d8e6ae8cfb3c94a61bd13596865e47f6fd476adeb1de5f6d6cc93a82d1aa8256/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f63796265782f6c61726176656c2d71756572792d7472616365722e737667)](https://packagist.org/packages/cybex/laravel-query-tracer)[![Github build status](https://github.com/cybex-gmbh/laravel-query-tracer/actions/workflows/tests.yml/badge.svg)](https://github.com/cybex-gmbh/laravel-query-tracer/actions/workflows/tests.yml/badge.svg)[![GitHub pull-requests](https://camo.githubusercontent.com/e032eb2cf1ce040442ee267d05869917863a5850161639c215d906e173eface8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d70722f63796265782d676d62682f6c61726176656c2d71756572792d7472616365722e737667)](https://github.com/cybex-gmbh/laravel-query-tracer/pull/)[![GitHub issues](https://camo.githubusercontent.com/3d2a968bf19743bee96af524b1ec79f00a63363459ae74020136ffdd2373fb65/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f63796265782d676d62682f6c61726176656c2d71756572792d7472616365722e737667)](https://github.com/cybex-gmbh/laravel-query-tracer/issues/)[![Laravel Version](https://camo.githubusercontent.com/26a4d433a918ec0c8b89f8b05263c1a3a0dec96b80f8f1f424cfc31003851d1b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4d696e696d756d5f4c61726176656c5f56657273696f6e2d372e782d7265642e737667)](https://laravel.com/docs/7.x)

[![Sample Query with Trace](media/query.png)](media/query.png)

Stack Traces for Your Queries.
------------------------------

[](#stack-traces-for-your-queries)

If you ever wondered where specific queries in your log originate from, or wanted to easily track down the code that caused a slow query, this package is for you.

*Laravel Query Tracer* hooks into your Eloquent and adds information from the most useful stack frame it finds, and adds not only the class, file, line number, function name and called function, but also the relevant part of the source file.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require cybex/laravel-query-tracer
```

Usage
-----

[](#usage)

To activate the Query Tracer, just add the following line to your `.env` file:

```
QUERY_TRACER_ENABLED=true

```

Once you have done this, all Model queries will contain a comment with useful information to locate the source of the query.

The easiest way to evaluate the queries is to install [Laravel Telescope](https://laravel.com/docs/8.x/telescope), the [Laravel Debugbar](https://github.com/barryvdh/laravel-debugbar) or any other package that will log and display queries.

Configuration
-------------

[](#configuration)

The `query-tracer.php` config file can be published using the following command:

```
php artisan vendor:publish --provider="Cybex\QueryTracer\QueryTracerServiceProvider" --tag="config"
```

You can customize a variety of options which are explained in the comments inside the configuration file.

Additionally, some options can also be set using environment settings that you can put in your .env file as needed.

The available options with their corresponding env settings and defaults are:

OptionEnvDefaultDescriptionenabledQUERY\_TRACER\_ENABLEDfalseMain switch for turning query tracer integration on/off.modeQUERY\_TRACER\_MODEdefaultDefault mode of operation. When set to `scoped`, it will run in [scoped mode](#scoped-mode), otherwise it runs in [default mode](#default-mode).modelNamespaceQUERY\_TRACER\_MODEL\_NAMESPACE''The path to your Models directory, relative to `/app`.restrictToDriverQUERY\_TRACER\_DRIVER'\*'Specify which driver Query Tracer will attach to (e.g. mysql, pgsql, sqlite). If set to '\*', it will attach to the first established connection.trace.logArray.enabledQUERY\_TRACER\_ENABLE\_ARRAY\_TRACEtrueIf true, a trace key will be added to Laravel's QueryLog with all the information requested in `logArray.values`.trace.logArray.keyQUERY\_TRACER\_LOG\_KEY'trace'Name of the key that will be used for adding the trace to the QueryLog.trace.sqlComment.enabledQUERY\_TRACER\_ENABLE\_COMMENT\_TRACEtrueIf true, the query trace will be added to the query as an SQL comment. In `default` mode, the comment is added AFTER execution for logging only. In `scoped` mode, it will be added to the query BEFORE execution.trace.sqlComment.tagQUERY\_TRACER\_TAG''If set, the SQL comments will carry this tag right after the opening comment token (`/*`).Modes of Operation
------------------

[](#modes-of-operation)

Query Tracer comes with two different modes of operation that are different in how they hook into the framework. If you are debugging your queries locally within [Laravel Telescope](https://laravel.com/docs/8.x/telescope) or the [Laravel Debugbar](https://github.com/barryvdh/laravel-debugbar), you will want to use `default` mode. If you want the traces to appear in your database server, for example inside a slow query log or any other server log, you will have to use `scoped` mode.

### Default Mode

[](#default-mode)

In `default` mode, *Query Tracer* overrides the logging method of Laravel's Database Connection class. In this mode, the SQL comments are added to the queries after they have been executed on your DB server, and thus will not appear on the server itself. Also, you can enable adding trace information to every QueryLog entry via `trace.logArray`:

```
       "query" => "select * from `pages` where `pages`.`parent_id` in (1, 2, 3, 4, 5, 6)",
       "bindings" => [],
       "time" => 0.41,
       "trace" => [
         "call" => "App\Models\Page::get()",
         "class" => "Illuminate\Database\Eloquent\Builder",
         "file" => "/app/Base/Helpers/ApplicationHelper.php",
         "function" => "{closure}()",
         "line" => 132,
         "source" => """
             0129      function getMenu()\n
             0130      {\n
             0131          return cache()->remember('menu', 60, function () {\n
           * 0132              return \App\Models\Page::where('parent_id', null)->with('children')->get();\n
             0133          });\n
             0134      }\n
             0135  }
           """,
       ],

```

By default, the key is called `trace`, but can be renamed using the `trace.logArray.key` option.

### Scoped Mode

[](#scoped-mode)

In `scoped` mode, *Query Tracer* attaches a global scope to each of your Models that will add a `where 1` to each query that attaches the SQL comment with the trace. Therefore, each query is modified before its being executed and will therefore be present in all logs along the way. Note that adding traces to the QueryLog will not work in `scoped` mode, because the log method will not the overridden.

Available Trace Values
----------------------

[](#available-trace-values)

The `query-tracer.trace.logArray.values` and `query-tracer.trace.sqlComment.template` options configure the values that will be included in the QueryLog array and the SQL comments, respectively. The available values are the same for both options; the only difference is that you need to precede them with `@` for use in the SQL template.

The following values are available for use:

### call

[](#call)

The call that triggered the query, including a synopsis of its arguments.

### class

[](#class)

The class of the object the call was executed on.

### compiled

[](#compiled)

Only set if the call is inside of a compiled Blade view. It then contains the file path of the compiled view, while `file` contains the name of the original Blade template.

### file

[](#file)

The file that contains the call, including the path relative to the project root.

### line

[](#line)

The number of the line that contains the call.

### source

[](#source)

The part of the source code that contains the call. The amount of code listed is configurable with the `query-tracer.trace.includeSourceLines` setting.

Custom Formatters
-----------------

[](#custom-formatters)

You can replace for formatters for source code, call arguments, QueryLog arrays and SQL comments with your own implementations by replacing the classes defined in the configuration.

### Argument Formatter

[](#argument-formatter)

The argument formatter converts the call arguments from the stack frame into a printable representation. The formatter class used is configured in `query-tracer.backtrace.argumentFormatter`. Argument formatters need to implement `Cybex\QueryTracer\Interfaces\ArgumentFormatterInterface`.

A very easy implementation of a custom argument formatter that only prints arguments' types could look like this:

```
