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

To get the query executed in Laravel 5, you can use the DB::getQueryLog() method. This method returns an array of all the queries run by the database connection.

Here is an example of how you can use it:


$queries = DB::getQueryLog();

foreach($queries as $query) {
    $query = $query['query'];
    $bindings = $query['bindings'];
    $time = $query['time'];
    // do something with the query, bindings, and time

Watch a course Learn object oriented PHP

Note that DB::getQueryLog() only works if the DB_DEBUG environment variable is set to true. This variable is set to true by default in the local environment configuration file.

If you are not getting any results from DB::getQueryLog(), it could be because the DB_DEBUG variable is set to false or because you are not running the code after a query has been executed.

You can also use the Laravel Debugbar package to easily view all the queries run by your application. This package will show you all the queries run by your application, along with their bindings and execution time.