How can I retrieve the raw executed SQL query in Laravel 3/4 using Laravel Query Builder or Eloquent ORM?
For example, something like this:
DB::table('users')->where_status(1)->get();
Or:
(posts (id, user_id, ...))
User::find(1)->posts->get();
Otherwise, at the very least how can I save all queries executed to laravel.log?
Laravel 5
Note that this is the procedural approach, which I use for quick debugging
in your header, use:
The output will look something like this (default log file is laravel.log):
***I know this question specified Laravel 3/4 but this page comes up when searching for a general answer. Newbies to Laravel may not know there is a difference between versions. Since I never see
DD::enableQueryLog()
mentioned in any of the answers I normally find, it may be specific to Laravel 5 - perhaps someone can comment on that.in Laravel 4 you can actually use an Event Listener for database queries.
Place this snippet anywhere, e.g. in
start/global.php
. It'll write the queries to the info log (storage/log/laravel.log
).Or as alternative to laravel 3 profiler you can use:
https://github.com/paulboco/profiler or https://github.com/barryvdh/laravel-debugbar
Last query print
For Eloquent you can just do:
But you need to remove the "->get()" part from your query.
Using the query log doesnt give you the actual RAW query being executed, especially if there are bound values. This is the best approach to get the raw sql:
or more involved: