Inspect Laravels query builder output

When learning something new (in this case Laravel and it’s ORM Eloquent) it is always nice to be able to “look behind the curtains” to see what actually happens. This both helps you understand the internals of the things you are using and can sometimes make bug squashing a lot easier.

I wanted to be able to see the SQL that Eloquents query builder was producing, because SQL is something I’m fairly proficient with.

I found several questions and answers on different approaches on StackOverflow but one approach stood out for me: Using Laravels internal events.

Every time Laravel does anything through a database connection an event i fired with the prepared statement and its bindings. But creating SQL queries from this still required some manual labor because I needed to replace all the question marks with the binding in order.

So to close this gap further I had the prepared SQL and bindings crunched by sprintf before writing it to a log file or stdout, like so:

\Event::listen(
    'illuminate.query',
    function($query, $params, $time, $conn)
    {
        $sql = str_replace('?', '%s', $query);
        $preppedSql = call_user_func_array('sprintf', array_merge([$sql], $params));

        echo $preppedSql . "\n"; // or write it to a log file
    });