Laravel Eloquent display query log
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
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.