John Main Logo

John Main

Code. Design. Hosting. Maintenance.

Dec '14

Those of you who do much work in Laravel will know that dumping the last query performed can be rather a pain. The classic way is to insert the following after the query you want to inspect:

$queries = \DB::getQueryLog();

However this is awkward, hard to remember, and gives you the bindings separately from the main query, leaving you to recombine them manually. After a little thought I decided the best thing was to write my own function that I could insert into /app/start/global.php and then have available to use from anywhere within my application:

 * Dump last query and exit
function dddb() {
    $queries = \DB::getQueryLog();
    $query_parts = end($queries);

    $query = $query_parts['query'];
    $bindings = $query_parts['bindings'];

    foreach($bindings as $binding) {
            $query = preg_replace('/\?/', $binding, $query, 1);
            $query = preg_replace('/\?/', "'" . $binding . "'", $query, 1);


Now all I have to do is call dddb(); from anywhere within my code to get the complete last executed query. No fuss, no pain :)