How to Get the Query Executed in Laravel 5? DB::getQueryLog() Returning Empty Array

How to Get the Query Executed in Laravel 5? DB::getQueryLog() Returning Empty Array

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why DB::getQueryLog() Returns Empty in Laravel 5

Encountering an empty array when calling DB::getQueryLog() after executing a query is a common stumbling block, particularly for developers transitioning from raw database interactions to the Laravel ORM. The code snippet below demonstrates the exact scenario: running a simple model fetch and expecting immediate visibility into the SQL generated.

$user = User::find(5);
print_r(DB::getQueryLog()); // Returns Array ( )

The root cause is not a bug in your application logic, but rather a specific configuration requirement within Laravel 5. By default, the query log is disabled to ensure maximum performance in production environments. Recording every SQL statement executed consumes memory and I/O resources, which can degrade application speed significantly under load. Consequently, DB::getQueryLog() will always return an empty collection unless you explicitly enable logging before executing your queries.

To resolve this, you must call the enableQueryLog method on the database manager instance. This should be done prior to the query execution. You can achieve this by injecting the DB facade or accessing the container's DB instance directly:

use Illuminate\Support\Facades\DB;

// Enable logging before running queries
DB::enableQueryLog();

$user = User::find(5);

// Now the log will contain the executed SQL
print_r(DB::getQueryLog());

Alternatively, if you are working within a controller or a service class where facades might feel verbose, you can access the underlying database connection manager via dependency injection. This approach provides slightly more granular control over the lifecycle of the logging session.

It is important to understand the implications of enabling this feature. While DB::getQueryLog() is invaluable for debugging complex Eloquent relationships or verifying that raw SQL queries are being constructed as expected, it should generally be restricted to development and staging environments. In a production setting, leaving logging enabled can lead to increased memory usage and slower response times, especially if your application handles high volumes of requests. If you must log in production, consider flushing the query log after each request or limiting the number of queries logged to prevent memory exhaustion.

Another frequent pitfall involves the lifecycle of the query log. The DB::getQueryLog() method returns a static collection that persists until it is cleared. In long-running scripts or background jobs (like Laravel Queues), the log can grow indefinitely, potentially causing an Out of Memory error. If you are processing a batch of items, ensure you clear the log periodically using DB::flushQueryLog() if memory usage becomes a concern.

// Clear the log to free up memory after a specific operation
DB::flushQueryLog();

For developers who need persistent logging beyond the current request lifecycle without manually managing the state, Laravel provides the log_queries_using_pdo option in the config/database.php file. However, this is typically reserved for advanced debugging scenarios where you want to write queries directly to a separate log file rather than keeping them in memory.

If you are still troubleshooting an issue where enabling the log doesn't seem to work as expected, verify that you haven't inadvertently reset the database connection or switched connections without re-enabling logging on the specific instance. In Laravel 5, the query log is tied to the specific PdoConnection instance active in memory.

For further details on debugging your application's performance bottlenecks and optimizing database interactions, resources like https://laravelcompany.com offer insights into best practices for maintaining a healthy Laravel ecosystem.

Our network