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 4+
In Laravel 4 and later, you have to call
DB::getQueryLog()
to get all ran queries.Or you can download a profiler package. I'd recommend barryvdh/laravel-debugbar, which is pretty neat. You can read for instructions on how to install in their repository.
Note for Laravel 5 users: You'll need to call
DB::enableQueryLog()
before executing the query. Either just above the line that runs the query or inside a middleware.Laravel 3
In Laravel 3, you can get the last executed query from an
Eloquent
model calling the static methodlast_query
on theDB
class.This, however, requires that you enable the
profiler
option inapplication/config/database.php
. Alternatively you could, as @dualed mentioned, enable theprofiler
option, inapplication/config/application.php
or callDB::profile()
to get all queries ran in the current request and their execution time.