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'
Laravel >= 5.5
Install via composer
$ composer require laratoolbox/query-viewer
$ php artisan vendor:publish --provider="LaraToolbox\QueryViewer\QueryViewerServiceProvider"
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.
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%'
\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%'
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);
});
$ composer test
Please see CHANGELOG for more information on what has changed recently.
If you discover any security related issues, please email instead of using the issue tracker.
Please see CONTRIBUTING for details.
- Semih ERDOGAN
- Dincer DEMIRCIOGLU
- All contributors
- The social image generated with banners.beyondco.de.
- This package generated using the melihovv/laravel-package-generator.
The MIT License (MIT). Please see License File for more information.