PHPackages                             laratoolbox/query-viewer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. laratoolbox/query-viewer

ActiveLibrary[Database &amp; ORM](/categories/database)

laratoolbox/query-viewer
========================

Get sql queries with bindings replaced

v0.1.0(5y ago)1951MITPHPPHP &gt;=7.0

Since Nov 29Pushed 5y ago2 watchersCompare

[ Source](https://github.com/laratoolbox/query-viewer)[ Packagist](https://packagist.org/packages/laratoolbox/query-viewer)[ RSS](/packages/laratoolbox-query-viewer/feed)WikiDiscussions main Synced today

READMEChangelog (2)Dependencies (3)Versions (4)Used By (0)

[![Social Image](social.jpeg)](social.jpeg)

[![GitHub Workflow Status](https://github.com/laratoolbox/query-viewer/workflows/Run%20tests/badge.svg)](https://github.com/laratoolbox/query-viewer/actions)[![Packagist](https://camo.githubusercontent.com/945935bf4a38009827240b1291ce2f9297640c57cfe9dd01a75bf01d3aa7b8ad/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c617261746f6f6c626f782f71756572792d7669657765722e737667)](https://packagist.org/packages/laratoolbox/query-viewer)[![Packagist](https://camo.githubusercontent.com/7d5343307d7ce4c7be2761d3c84360f6e179d9789b302282944abb6cd42af6a6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6c617261746f6f6c626f782f71756572792d7669657765722e737667)](https://packagist.org/packages/laratoolbox/query-viewer)[![GitHub issues](https://camo.githubusercontent.com/563e66e4673fd0bb5c05d8163d1fbd99afe869b1265c99956aff127341c9552a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f6c617261746f6f6c626f782f71756572792d7669657765722e737667)](https://github.com/laratoolbox/query-viewer/issues)

Package description
-------------------

[](#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
-----------

[](#requirement)

```
Laravel >= 5.5

```

Build History
-------------

[](#build-history)

[![Build History](https://camo.githubusercontent.com/03796a14a6323bf73d300d4a6c099c48c79ffb4320c9b638f80f4e6148681ecf/68747470733a2f2f6275696c6473746174732e696e666f2f6769746875622f63686172742f6c617261746f6f6c626f782f71756572792d7669657765723f6272616e63683d6d61696e)](https://github.com/laratoolbox/query-viewer/actions)

Installation
------------

[](#installation)

Install via composer

```
$ composer require laratoolbox/query-viewer
```

### Publish package config

[](#publish-package-config)

```
$ php artisan vendor:publish --provider="LaraToolbox\QueryViewer\QueryViewerServiceProvider"
```

Usage
-----

[](#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
--------

[](#examples)

#### Eloquent

[](#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

[](#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

[](#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
-------

[](#testing)

```
$ composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Security
--------

[](#security)

If you discover any security related issues, please email instead of using the issue tracker.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

- [Semih ERDOGAN](https://github.com/semiherdogan)
- [Dincer DEMIRCIOGLU](https://github.com/dinncer)
- [All contributors](https://github.com/laratoolbox/query-viewer/graphs/contributors)
- The social image generated with [banners.beyondco.de](https://banners.beyondco.de/).
- This package generated using the [melihovv/laravel-package-generator](https://github.com/melihovv/laravel-package-generator).

License
-------

[](#license)

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

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 89.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

2

Last Release

2040d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5646041?v=4)[Semih ERDOGAN](/maintainers/semiherdogan)[@semiherdogan](https://github.com/semiherdogan)

![](https://avatars.githubusercontent.com/u/3058102?v=4)[Dinçer Demircioğlu](/maintainers/dinncer)[@dinncer](https://github.com/dinncer)

---

Top Contributors

[![semiherdogan](https://avatars.githubusercontent.com/u/5646041?v=4)](https://github.com/semiherdogan "semiherdogan (49 commits)")[![dinncer](https://avatars.githubusercontent.com/u/3058102?v=4)](https://github.com/dinncer "dinncer (6 commits)")

---

Tags

eloquentlaravellaravel-ormlaravel-query-buildersql-querylaravelqueryViewer

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/laratoolbox-query-viewer/health.svg)

```
[![Health](https://phpackages.com/badges/laratoolbox-query-viewer/health.svg)](https://phpackages.com/packages/laratoolbox-query-viewer)
```

###  Alternatives

[illuminate/database

The Illuminate Database package.

2.8k54.9M11.6k](/packages/illuminate-database)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8793.2M25](/packages/yajra-laravel-oci8)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k5.1M34](/packages/tucker-eric-eloquentfilter)[glushkovds/phpclickhouse-laravel

Adapter of the most popular library https://github.com/smi2/phpClickHouse to Laravel

2051.5M1](/packages/glushkovds-phpclickhouse-laravel)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.3M18](/packages/reedware-laravel-relation-joins)[lemaur/eloquent-publishing

218.1k1](/packages/lemaur-eloquent-publishing)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
