Skip to content

Bind parameters into sql query for laravel eloquent and database query builder.

License

Notifications You must be signed in to change notification settings

independent-creditview/query-viewer

 
 

Repository files navigation

Social Image

GitHub Workflow Status Packagist Packagist GitHub issues

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

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

License

The MIT License (MIT). Please see License File for more information.

About

Bind parameters into sql query for laravel eloquent and database query builder.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%