In this Laravel tutorial “Laravel 12 Print Last Executed Query with Parameters”, you’ll learn how to get the last executed SQL query in Laravel 12 using DB::enableQueryLog(), getQueryLog(), toSql(), dd(), listen(), ddRawSql(), end(). This is helpful for debugging or understanding what queries are being run behind the scenes. Laravel offers several ways to view SQL queries, also known as the query log.
Many times, we need to retrieve the executed query log, get the last executed query, or display the SQL query from Laravel’s query builder. Laravel provides simple ways to achieve this.
Method 1: Using dd() to Get Last Executed Query Log
public function getData()
{
$query = Post::where('id', 1)->dd();
dd( $query);
}
The output is :
"select * from `posts` where `id` = ?"
array:1 [▼
0 => 1
]
Method 2: Using ddRawSql() to Get Last Executed Query Log
The ddRawSql() method can be used on a query to output the full SQL statement with all parameter bindings correctly substituted.
public function getData()
{
$query = Post::where('id', 1)->ddRawSql();
dd( $query);
}
The output is
"select * from `posts` where `id` = 1"
Example 3: Using DB::enableQueryLog() to Get Last Executed Query Log
To get Query Log From this method first use DB::enableQueryLog() to enable Query Log then to Print last executed query log use this method DB::getQueryLog($query).
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
use DB;
class DemoController extends Controller
{
public function getData(Request $request)
{
DB::enableQueryLog();
$query = Post::where('id', 1)->first();
dd( DB::getQueryLog($query));
}
}
The output is :
array:1 [▼ // app\Http\Controllers\DemoController.php:16
0 => array:3 [▼
"query" => "select * from `posts` where `id` = ? limit 1"
"bindings" => array:1 [▼
0 => 1
]
"time" => 86.66
]
]
Method 4: Using DB::enableQueryLog(),end($query) to Get Last Executed Query Log
This function give you similar output as method 3
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
use DB;
class DemoController extends Controller
{
public function getData(Request $request)
{
DB::enableQueryLog();
$query = Post::where('id',1)->first();
$query = DB::getQueryLog();
dd(end($query));
}
}
The Output is
array:3 [▼ // app\Http\Controllers\DemoController.php:16
"query" => "select * from `posts` where `id` = ? limit 1"
"bindings" => array:1 [▼
0 => 1
]
"time" => 1.24
]
Method 5: Using DB::listen() to Get Last Executed Query Log
If you want to execute a custom action every time an SQL query runs in your application, you can use the DB
facade’s listen
method. This is especially helpful for logging queries or debugging database interactions during development. It is important to use json_encode($query->bindings) when logging query bindings; otherwise, the Log::info() function may throw an error.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
//
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
DB::listen(function ($query) {
Log::info('SQL'.$query->sql);
Log::info('Bindings'.json_encode($query->bindings));
Log::info('time'.$query->time);
Log::info('Raw SQL'.$query->toRawSql());
});
}
}
The output is storage/logs/laravel.log
[2025-07-09 09:08:09] local.INFO: SQLselect * from `sessions` where `id` = ? limit 1
[2025-07-09 09:08:09] local.INFO: Bindings["9NAaUj1e7ykKaNUBAOHcSwxGc2zNCYYgaNMMorFR"]
[2025-07-09 09:08:09] local.INFO: time9.26
[2025-07-09 09:08:09] local.INFO: Raw SQLselect * from `sessions` where `id` = '9NAaUj1e7ykKaNUBAOHcSwxGc2zNCYYgaNMMorFR' limit 1