How to Enable and Get Query Log in Laravel

How to Enable and Get Query Log in Laravel

The GetQueryLog() method is typically used in a development or debugging environment to gain insight into underlying database activity. This enables you to see the actual SQL queries generated by your application, the associated bindings, and the order in which they are executed.

In Laravel, the getQueryLog() method allows you to retrieve database queries executed during a request. It provides a convenient way to debug and analyze the SQL queries being executed by your application.

By calling DB::getQueryLog(), Laravel returns an array containing all executed queries along with their bindings. This information is particularly useful for identifying potential performance issues, optimizing queries, or understanding your application’s database interactions.

To enable and get query log using getQueryLog(), follow these steps:

Enable Query Logging: In your Laravel application, query logging is disabled by default. To enable query log for print last executed query in laravel, you can add the following line of code to your AppServiceProvider or any other suitable service provider:


Execute queries: Perform database operations that you want to monitor or debug, such as fetching data, inserting records, or updating information.

Retrieve Query Log: After executing the desired query, you can retrieve the executed queries and their bindings by using getQueryLog() method. For example:

$queryLog = \DB::getQueryLog()

Analyze the query log: The $queryLog variable now contains an array of executed queries. Each query in the log is represented as an associative array with the following information:

  • query: The actual SQL query executed.
  • bindings: An array of parameter bindings used in the query.
  • time: The execution time of the query.

You can loop through the $queryLog array to access and analyze individual queries or their properties. For example, you can log queries to your application’s log files or output them to the browser console for further investigation.

It’s important to note that to ensure accurate results, you should call getQueryLog() immediately after executing the desired queries and before executing any unrelated database operations.

Overall, the getQueryLog() method in Laravel provides a powerful tool for understanding and optimizing your application’s database interactions, making it easy to debug and fine-tune your database queries.

For example, if you want to print last executed query log in laravel, you can:

$user = User::get()
$query = DB::getQueryLog()

Recommended Laravel Tutorials


My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *