Get the last query executed in Laravel

Sometimes it's useful for debugging purposes to preview the query that Laravel generates with Eloquent/Fluent.

Laravel 5

(!) In Laravel 5 you should enable query logging by:

DB::enableQueryLog();

Then after placing your final statement:

DB::table('users')->where('name', '=', 'Aknavi')->get();

just run:

dd(DB::getQueryLog()); - it will return the SQL and the bindings of the last queries that were executed.

Laravel 4

In Laravel 4 queryLogging is enabled by default, so you can just dump the query log:

dd(DB::getQueryLog());

Laravel 3

In Laravel 3 you can run:

DB::last_query();

But you must enable the profiler, first (application/config/database.php - profiler).

Or you can use the profiler to get all queries executed for the current request and their execution time.


unsplash-logoAron Visuals provided this incredible picture!

Show Comments