Coder Social home page Coder Social logo

laratoolbox / query-viewer Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 1.0 123 KB

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

Home Page: https://packagist.org/packages/laratoolbox/query-viewer

License: MIT License

PHP 100.00%
laravel laravel-query-builder eloquent laravel-orm sql-query

query-viewer's Introduction

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.

query-viewer's People

Contributors

dinncer avatar semiherdogan avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.