laratoolbox / query-viewer
Get sql queries with bindings replaced
Requires
- php: >=7.0
- illuminate/support: ^5.5|^5.6|^5.7|^5.8|^6.0|^7.0|^8.0
Requires (Dev)
- orchestra/testbench: ^3.5|^3.6|^3.7|^3.8|^4.0|^5.0|^6.0
- phpunit/phpunit: ~6.0|^7.0|^7.5|^8.4|^9.0
README
Package description
This package adds custom methods to eloquent and database query builder for getting sql query. (question marks replaced with values)
Laravel's toSql()
method gives you sql query without bindings replaced. (see question marks below)
\DB::table('users')->select('name')->where('id', 5)->toSql(); // select `name` from `users` where `id` = ? and `name` = ?
With this package you have getSql()
method. Which gives you same result with bindings replaced.
\DB::table('users')->select('name')->where('id', 5)->getSql(); // select `name` from `users` where `id` = 5 and `name` = 'laravel'
Requirement
Laravel >= 5.5
Build History
Installation
Install via composer
$ composer require laratoolbox/query-viewer
Publish package config
$ php artisan vendor:publish --provider="LaraToolbox\QueryViewer\QueryViewerServiceProvider"
Usage
After installing this package you can use these methods on eloquent and database builder.
-
getSql
- This method returns sql query.
- Optionally takes closure as parameter and calls closure with sql string.
- Returns string or closure result.
-
dumpSql
- This method prints sql query (uses dump() function)
- Returns builder.
-
logSql
- This method logs sql query.
- Optionally takes prefix string parameter. Which prepends to sql string.
- Log type can be set in config file (default is "info").
- Returns builder.
Examples
Eloquent
use App\Models\User; User::select('name')->where('id', 5)->getSql(); // 'select `name` from `users` where `id` = 5' User::select('name') ->where('id', 5) ->dumpSql() // PRINTS: select `name` from `users` where `id` = 5 ->logSql('LOG_PREFIX_HERE') // logs sql to log file. (LOG_PREFIX_HERE : select `name` from `users` where `id` = 5) ->where('name', '!=', 'john') ->dumpSql() // PRINTS: select `name` from `users` where `id` = 5 and `name` != 'john' ->where('surname', '!=', 'doe') ->where('email', 'LIKE', '%example%') ->getSql(function(string $sql) { echo $sql; // select `name` from `users` where `id` = 5 and `name` != 'john' and `surname` != 'doe' and `email` LIKE '%example%' }) ->getSql(); // PRINTS: select `name` from `users` where `id` = 5 and `name` != 'john' and `surname` != 'doe' and `email` LIKE '%example%'
Database Builder
\DB::table('users')->select('name')->where('id', 5)->getSql(); // 'select `name` from `users` where `id` = 5' \DB::table('users') ->where('id', 5) ->dumpSql() // PRINTS: select `name` from `users` where `id` = 5 ->logSql('LOG_PREFIX_HERE') // logs sql to log file. (LOG_PREFIX_HERE : select `name` from `users` where `id` = 5) ->where('name', '!=', 'john') ->dumpSql() // PRINTS: select `name` from `users` where `id` = 5 and `name` != 'john' ->where('surname', '!=', 'doe') ->where('email', 'LIKE', '%example%') ->getSql(function(string $sql) { echo $sql; // select `name` from `users` where `id` = 5 and `name` != 'john' and `surname` != 'doe' and `email` LIKE '%example%' }) ->getSql(); // PRINTS: select `name` from `users` where `id` = 5 and `name` != 'john' and `surname` != 'doe' and `email` LIKE '%example%'
Replace bindings for all queries
Add code below into app/Providers/AppServiceProvider.php
file for listening all queries.
use LaraToolbox\QueryViewer\QueryViewer; \DB::listen(function ($query) { $sql = QueryViewer::replaceBindings($query->sql, $query->bindings); logger()->info($sql); });
Testing
$ composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Security
If you discover any security related issues, please email instead of using the issue tracker.
Contributing
Please see CONTRIBUTING for details.
Credits
- Semih ERDOGAN
- Dincer DEMIRCIOGLU
- All contributors
- The social image generated with banners.beyondco.de.
- This package generated using the melihovv/laravel-package-generator.
License
The MIT License (MIT). Please see License File for more information.