How to Enable Query Log in Laravel

How to Enable Query Log in Laravel

To enable and get query log in laravel; Just use enableQueryLog to enable query log and use DB::getQueryLog() to print log in laravel.

By default, Laravel’s query builder has two enableQueryLog and getQueryLog methods that you can use to get all the information related to the query for analysis, debugging, optimization, etc.

How to Enable and Get Query Log in Laravel

Steps to enable and get query log in laravel 11, 10, 9:

1. Enable Query Logging: In Laravel application, query logging is disabled by default. To enable query log for print last executed query in laravel, you can do it by using \DB::enableQueryLog() method on laravel, for example:


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

2. Get Query Log: After executing the desired query, you can get or 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 get or print last executed query log in Laravel, you can:

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

Recommended Laravel Tutorials


Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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