├── .gitignore ├── .travis.yml ├── README.md ├── composer.json ├── phpunit.xml ├── src ├── Providers │ └── QueryTracerServiceProvider.php └── Scopes │ └── QueryTracer.php └── tests └── QueryTracerTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | 3 | /vendor 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.5.9 5 | - 5.6 6 | - 7.0 7 | 8 | before_script: 9 | - composer self-update 10 | 11 | install: 12 | - composer install --prefer-source --no-interaction --dev 13 | 14 | script: vendor/bin/phpunit 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Laravel Query Tracer 2 | 3 | [![Build Status](https://travis-ci.org/fitztrev/query-tracer.svg?branch=master)](https://travis-ci.org/fitztrev/query-tracer) [![Latest Stable Version](https://poser.pugx.org/fitztrev/query-tracer/v/stable)](https://packagist.org/packages/fitztrev/query-tracer) 4 | 5 | Find exactly *where* a specific database query is being called in your Laravel application. 6 | 7 | Want to optimize or debug your database queries but not sure where they're being called? See how it works below with [Clockwork](https://github.com/itsgoingd/clockwork): 8 | 9 | ![](http://i.imgur.com/0cRs7TU.png) 10 | 11 | If you use [Debugbar](https://github.com/barryvdh/laravel-debugbar), it has this functionality built-in if you set `debugbar.options.db.backtrace` to `true`. 12 | 13 | ### Installation 14 | 15 | 1. Install via composer: 16 | 17 | ```bash 18 | composer require fitztrev/query-tracer 19 | ``` 20 | 21 | 2. Add the service provider to your `config/app.php`: 22 | 23 | ```php 24 | 'providers' => [ 25 | // ... 26 | Fitztrev\QueryTracer\Providers\QueryTracerServiceProvider::class, 27 | ], 28 | ``` 29 | 30 | ### How does it do it? 31 | 32 | It makes use of Laravel's global query scopes to do a backtrace and find where a query originated. Then it puts that info in extraneous but helpful `WHERE` clauses. 33 | 34 | By default, it's only enabled when `debug` is on. You can change this behavior for specific models by adding an `enableQueryTracer()` method to your model(s). For example: 35 | 36 | ```php 37 | public function enableQueryTracer() 38 | { 39 | return config('app.env') == 'local'; 40 | } 41 | ``` 42 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fitztrev/query-tracer", 3 | "description": "Backtrace database queries", 4 | "license": "MIT", 5 | "keywords": ["laravel", "debug", "database", "eloquent"], 6 | "require": { 7 | "php": ">=5.5.9", 8 | "laravel/framework": "^5.2" 9 | }, 10 | "require-dev": { 11 | "illuminate/database": "5.2.*", 12 | "orchestra/testbench": "~3.0", 13 | "phpunit/phpunit": "~4.1" 14 | }, 15 | "autoload": { 16 | "psr-4": { 17 | "Fitztrev\\QueryTracer\\": "src/" 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/Providers/QueryTracerServiceProvider.php: -------------------------------------------------------------------------------- 1 | listen('eloquent.booted: *', function ($model) { 19 | $model->addGlobalScope(new QueryTracer); 20 | }); 21 | } 22 | 23 | /** 24 | * Register the application services. 25 | * 26 | * @return void 27 | */ 28 | public function register() 29 | { 30 | // 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Scopes/QueryTracer.php: -------------------------------------------------------------------------------- 1 | enableQueryTracer(); 16 | } 17 | 18 | return config('app.debug'); 19 | } 20 | 21 | public function apply(Builder $builder, Model $model) 22 | { 23 | if (! $this->isEnabled($model)) { 24 | return; 25 | } 26 | 27 | $traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); 28 | 29 | foreach ($traces as $trace) { 30 | // Find the first non-vendor-dir file in the backtrace 31 | if (isset($trace['file']) && ! str_contains($trace['file'], DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR)) { 32 | $file = '"query.file" <> "' . $trace['file'] . '"'; 33 | $line = '"query.line" <> "' . $trace['line'] . '"'; 34 | 35 | return $builder->whereRaw($file)->whereRaw($line); 36 | } 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/QueryTracerTest.php: -------------------------------------------------------------------------------- 1 | true]); 11 | $model = new QueryTracerDefaultTestModel(); 12 | $query = $model->newQuery(); 13 | $this->assertEquals('select * from "table" where "query.file" <> "'.__FILE__.'" and "query.line" <> "'.__LINE__.'"', $query->toSql()); 14 | } 15 | 16 | public function testQueryTracerIsDisabledWhenDebugIsOff() 17 | { 18 | config(['app.debug' => false]); 19 | $model = new QueryTracerDefaultTestModel(); 20 | $query = $model->newQuery(); 21 | $this->assertEquals('select * from "table"', $query->toSql()); 22 | } 23 | 24 | public function testQueryTracerIsExplicitlyEnabled() 25 | { 26 | $model = new QueryTracerEnabledTestModel(); 27 | $query = $model->newQuery(); 28 | $this->assertEquals('select * from "table" where "query.file" <> "'.__FILE__.'" and "query.line" <> "'.__LINE__.'"', $query->toSql()); 29 | } 30 | 31 | public function testQueryTracerIsExplicitlyDisabled() 32 | { 33 | $model = new QueryTracerDisabledTestModel(); 34 | $query = $model->newQuery(); 35 | $this->assertEquals('select * from "table"', $query->toSql()); 36 | } 37 | 38 | public function testQueryTracerCanBeRemoved() 39 | { 40 | $model = new QueryTracerEnabledTestModel(); 41 | $query = $model->newQuery()->withoutGlobalScope(QueryTracer::class); 42 | $this->assertEquals('select * from "table"', $query->toSql()); 43 | } 44 | 45 | protected function getEnvironmentSetUp($app) 46 | { 47 | // Setup testing database 48 | $app['config']->set('database.default', 'testbench'); 49 | $app['config']->set('database.connections.testbench', [ 50 | 'driver' => 'sqlite', 51 | 'database' => ':memory:', 52 | 'prefix' => '', 53 | ]); 54 | } 55 | 56 | protected function getPackageProviders($app) 57 | { 58 | return [Fitztrev\QueryTracer\Providers\QueryTracerServiceProvider::class]; 59 | } 60 | } 61 | 62 | class QueryTracerDefaultTestModel extends Illuminate\Database\Eloquent\Model 63 | { 64 | protected $table = 'table'; 65 | } 66 | 67 | class QueryTracerEnabledTestModel extends Illuminate\Database\Eloquent\Model 68 | { 69 | protected $table = 'table'; 70 | 71 | public function enableQueryTracer() 72 | { 73 | return true; 74 | } 75 | } 76 | 77 | class QueryTracerDisabledTestModel extends Illuminate\Database\Eloquent\Model 78 | { 79 | protected $table = 'table'; 80 | 81 | public function enableQueryTracer() 82 | { 83 | return false; 84 | } 85 | } 86 | --------------------------------------------------------------------------------