0

In Illuminate/Database/Connection.php I can see:

/**
 * Indicates whether queries are being logged.
 *
 * @var bool
 */
protected $loggingQueries = false;

/**
 * Enable the query log on the connection.
 *
 * @return void
 */                                                                                                                                              public function enableQueryLog()
{
    $this->loggingQueries = true;
}

/**
 * Disable the query log on the connection.
 *
 * @return void
 */
public function disableQueryLog()
{
    $this->loggingQueries = false;
}

/**
 * Determine whether we're logging queries.
 *
 * @return bool
 */
public function logging()
{
    return $this->loggingQueries;
}

/**
 * Run a SQL statement and log its execution context.
 *
 * @param  string    $query
 * @param  array     $bindings
 * @param  \Closure  $callback
 * @return mixed
 *
 * @throws \Illuminate\Database\QueryException
 */
protected function run($query, $bindings, Closure $callback)
{
    ...

    $this->logQuery($query, $bindings, $time);

    return $result;
}

/**
 * Log a query in the connection's query log.
 *
 * @param  string  $query
 * @param  array   $bindings
 * @param  float|null  $time
 * @return void                                                                                                                                   */
public function logQuery($query, $bindings, $time = null)                                                                                        {
    ...

    if ($this->loggingQueries) {                                                                                                                         $this->queryLog[] = compact('query', 'bindings', 'time');
    }
}

Does this data get stored somewhere? How do I enable it globally, if so?

1 Answer 1

2

I would use middleware to enable it. This would allow you to enable with options (like only in local environments, etc).

You could gather the information with dd() or using Log::info(), etc. Something like:

namespace App\Http\Middleware;

use DB;
use Log;
use Closure;

class EnableQueryLogMiddleware
{
    public function handle($request, Closure $next)
    {
        if (env('local')) {
            DB::enableQueryLog();
        }

        return $next($request);
    }

    public function terminate($request, $response)
    {
        // Here you can either Log it, DD, etc.
        dd(DB::getQueryLog());
        Log::info(DB::getQueryLog());
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

So there's no easy way, like specifying some parameter in config/database.php?
I feel creating middleware like above is easy, takes < 5 mins to enable. To me, this is the easiest way but that's just my opinion. You can use DB::enableQueryLog(); anywhere in your application, which enables the ability to log the queries, so in theory you could just place it in your app/bootstrap/app.php file. Not elegant but I believe it would work.
Just confirming. I hoped, it can be used just by changing some configuration parameter. But it turned out it needs some more effort than that. Not much though. And I probably agree that using middleware is generally a better option.
Yeah, I agree it would be nice to have it as a parameter in config/database.php but seems not possible at this point. Maybe in future releases!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.