Skip to content

Instantly share code, notes, and snippets.

@devig
Forked from Ellrion/helper.php
Last active February 29, 2020 12:38

Revisions

  1. devig revised this gist Feb 29, 2020. No changes.
  2. @Ellrion Ellrion revised this gist Jul 31, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions helper.php
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,8 @@

    if (! function_exists('dqd')) {
    /**
    * Dump info about query in builder and end the script.
    *
    * @param $query
    * @param bool $short
    */
  3. @Ellrion Ellrion created this gist Jul 31, 2017.
    26 changes: 26 additions & 0 deletions helper.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    <?php

    if (! function_exists('dqd')) {
    /**
    * @param $query
    * @param bool $short
    */
    function dqd($query, $short = false)
    {
    if ($query instanceof \Illuminate\Database\Eloquent\Builder) {
    $query = $query->getQuery();
    }
    $sql = $query->toSql();
    $bindings = array_map(function ($binding) {
    return is_int($binding) || is_float($binding) ? $binding : "'{$binding}'";
    }, $query->getBindings());
    $raw = vsprintf(str_replace(['%', '?'], ['%%', '%s'], $sql), $bindings);
    $bindings = $query->getRawBindings();

    if ($short) {
    dd($raw);
    }

    dd(compact('sql', 'bindings', 'raw'));
    }
    }