While building applications, debugging database queries is something that usually comes across. At times, you may need to know what SQL query really executes in the background, particularly when utilizing Eloquent ORM or advanced query builders. In Laravel 12, it is easy to retrieve the last executed SQL query to see how your conditions, joins, or filters get translated into SQL.
Suppose you are creating a user management system and some of the records are not showing up as intended. Rather than second-guessing, you can verify the last query executed in Laravel 12 to check exactly what is being queried to the database. This allows you to instantly tell whether missing conditions, faulty bindings, or syntax are occurring in your query.
Get Last Executed SQL Query Using Query Log
The Query Log method is one of the simple and easy to capture executed queries. It records all SQL queries performed during a request, allowing you to inspect them afterward. It uses DB facade and you can specify when to start logging query.
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Models\User;
class TestQuery extends Controller
{
public function index()
{
DB::enableQueryLog();
User::where('status', 'active')->get();
$queries = DB::getQueryLog();
$lastQuery = end($queries);
dd($lastQuery);
}
}
With DB::enableQueryLog()
, you are starting query logger and with get method you are getting executed query between both methods. Using end function to get last query only.
Getting Last SQL Query Using toSql()
The toSql() also can be used to get last executed query in Laravel 12. Instead of featching data from Eloquent query builder we will just print it. It’s a great way to preview the query before it runs. It’s idle while generating complex SQL query using Eloquent feature in Laravel 12.
<?php
namespace App\Http\Controllers;
use App\Models\User;
class TestQuery extends Controller
{
public function index()
{
$query = User::where('email', 'like', '%example.com%');
dd($query->toSql());
}
}
This method does not perform actual operation to database and can be used to verify query structure quickly without hitting the database.
Get Executed Query in Laravel Using DB::listen()
The DB::listen() method listens to all queries executed by Laravel during a request. This approach is useful when you want to monitor every query happening in real time.
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Models\User;
class TestQuery extends Controller
{
public function index()
{
DB::listen(function ($query) {
dump($query->sql);
dump($query->bindings);
dump($query->time);
});
User::where('role', 'admin')->get();
}
}
It does not specifically target last query but focus on printing SQL query data for each query executed in between in Laravel 12 code execution.
Get Query Without Execution Using DB::pretend()
If you want to see the SQL generated by Laravel without actually running it, the DB::pretend() method is the right choice.
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Models\User;
class TestQuery extends Controller
{
public function index()
{
$queries = DB::pretend(function () {
User::where('status', 'inactive')->get();
});
dd($queries);
}
}
This method is also pefect for testing or analyzing queries without affecting your actual database.
Conclusion
Having access to the last run SQL query in Laravel 12 is an excellent debugging feature for developers. Each of these methods – Query Log
, toSql()
, listen()
, and pretend()
provides a different way to examine or analyze your queries.
If you want a more visual and detailed way to inspect queries without editing your code each time, you can use Laravel Debugbar. It displays all executed queries, their execution time, and bindings right in your browser. For a complete guide, check out our post Debug Laravel Application with Laravel Debugbar.