Laravel Eloquent display query log

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: An Introduction to Displaying Eloquent Query Logs in Laravel Applications

Introduction

Laravel is an incredibly popular PHP framework that has gained a lot of traction due to its clean and expressive syntax, extensive documentation, and rich community support. One key feature that helps make it so efficient is Laravel's Eloquent ORM (Object Relational Mapping). Eloquent allows developers to easily interact with your application’s database through a simple object-oriented interface. However, it's essential to understand how these queries are built and executed. This blog post will teach you how to display the query log generated by Laravel Eloquent so you can gain better insights into the database interactions in your applications.

The getQueryLog() Method

To display the logs of the queries that were run using Laravel's Eloquent, there is a method called `getQueryLog()`. This function is available on any query builder instance or model object that has been through some database interactions. You can simply call this method to print out all the SQL statements executed by your application. Let's walk you through an example:

Example Code

For this example, we will use a simple Order model and controller. First, let's create the Order model by extending the Eloquent model in Laravel with the `App\Order` namespace. Your code should look like this: ```php namespace App; use Illuminate\Database\Eloquent\Model; class Order extends Model { // Define your database table, columns and relationships here... } ``` Next, we will create a controller to demonstrate the use of `getQueryLog()`. Here is an example:
namespace App\Http\Controllers;

use App\Order;

class OrdersController extends Controller {
    public function show(Order $order) {
        $data = $order->all();
        return dd($order->getQueryLog());
    }
}
In the above example, we are accessing an order with a specific ID and loading all its related data. Once this is done, you can see the queries performed through `dd()`. Note that the `dd()` function stands for dump and die; it's used to display variables, which in this case are printing the query log.

Discussing the Method

The getQueryLog() method returns an array of all the SQL statements executed by your application. It can be of great help when debugging database queries or optimizing SQL performance. It gives you a clear picture of how your database is being used and what queries are being generated.

When to Use getQueryLog()?

As mentioned earlier, displaying the query log using `getQueryLog()` can be helpful in multiple scenarios: - Debugging database queries: If you're facing issues with your application performance or database errors, displaying the query logs will help pinpoint the issue and allow you to optimize or fix any problems. - Optimizing SQL performance: By viewing the actual SQL statements generated by Eloquent, you can identify areas for improvement and better optimization. - Ensuring correctness of queries: If you're working with a large and complex database or a new developer unfamiliar with your application's database structure, displaying query logs helps ensure that all queries are correctly written and executed to prevent any possible data loss or inconsistencies.

Conclusion

Displaying the Eloquent query log in Laravel is an essential skill for developers to understand their application's database interactions, identify potential issues, and optimize performance. By following this blog post, you'll be well equipped with the knowledge on how to use the `getQueryLog()` method effectively and gain insights into your database operations. Remember that debugging can also involve other tools, such as logging or real-time monitoring services like Laravel Vapor and Laradock. Always strive for continuous improvement and learning in the world of web development!