How to Get the Query Executed in Laravel 5? DB::getQueryLog() Returning Empty Array
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Laravel is a highly versatile PHP framework that offers various tools for managing database interactions. One of these tools is the DB facade, which provides a unified interface to work with multiple databases. However, occasionally you may encounter issues while trying to view the query log - especially when using DB::getQueryLog() returns an empty array. This post will guide you through understanding and fixing such problems.
Checking Your Configuration
First, ensure your Laravel application has appropriate configuration settings for logging queries. Open the config/database.php file to view the default logger name. It should be set to "sql" unless you have customized it in your project.
Common Pitfalls when Using DB::getQueryLog()
There are several common mistakes that may lead to an empty query log returned by the DB::getQueryLog() method. Here's a list of potential reasons:
config/database.php. If it's a custom one, verify that you have properly implemented your custom logger class for capturing database queries.
3. Modifying/Extending Laravel's QueryBuilder: If you are using extensions or custom modifications to the Laravel QueryBuilder, check whether they affect query logging. Sometimes, these changes may cause issues with the default query logging system. Try reverting those changes or use an alternative query logging solution for your requirements.
4. Manual Queries outside of Eloquent: While DB facade provides a more flexible way to write queries, Eloquent is usually preferred due to its built-in query logging capabilities. Be mindful of this difference and ensure you're working within the Eloquent framework when executing database operations.
5. Caching Queries or Using Transactional Database Connections: When using cache for your queries, they will be cached and not logged. Similarly, if you are utilizing transactional database connections, any query executed inside a transaction may not get logged. In such cases, you must disable caching for the queries of interest or modify the transactional behavior accordingly.
Debugging Techniques
If DB::getQueryLog() still returns an empty array after verifying the configuration and common pitfalls, consider debugging your application to narrow down the problem:
1. Manually Execute Queries: Try executing your queries manually using theDB::connection()->getPdo()->query() method. This can be helpful to ensure that the query is properly formed and executed.
2. Use Laravel's Query Logging Middleware: You can enable Laravel's built-in query logging middleware by adding the \Illuminate\Database\QueryLogMiddleware::class to your HTTP kernel's protected $middleware property or in the app/Http/Kernel.php file. This will automatically log all queries executed through Eloquent models, which could help you identify any issues with query logging.
3. Inspect Log Files: Laravel utilizes various log files for different purposes, including query logs. If you're still unable to locate the query logs in DB::getQueryLog(), check your storage/logs directory or modify the log channel settings in the config/logging.php file.
4. Utilize Third-Party Query Logging Packages: In some cases, Laravel's built-in query logging may not be sufficient for complex projects or specialized use cases. Consider exploring third-party packages like Monolog (https://packagist.org/packages/barryvdh/laravel-debugbar), which can provide more advanced query logging features and analysis tools.
Conclusion
Viewing the log for a database query in Laravel 5 can sometimes be tricky, especially when using DB::getQueryLog() returns an empty array. Follow these steps to understand why this occurs and resolve any issues you may encounter:
1. Ensure your configuration is correct for logging queries. 2. Debug the query execution process to identify potential problems. 3. Inspect log files or use third-party packages if necessary. 4. Always maintain a consistent approach when working with Laravel's database tools. Remember that effective troubleshooting and debugging skills will help you overcome any challenges encountered while working with Laravel's database features.