0

Thinking about moving to use Laravel with Eloquent, but I know given our site eventually we'll need to ask, "So what raw SQL query is Eloquent actually generating?" so we can troubleshoot that query to see what went wrong... and I don't see that info in the docs.

So how would we pull up the query that Eloquent generated?

1 Answers1

1

Fastest way is to add this to your route, controller action or even app/start/global.php:

DB::listen(function($sql, $bindings, $time) { 
    Log::info($sql);
});

It log it, so you can then

php artisan tail

To see your queries.

If you need to log just one query direct to the page it's almost the same, you just put it in the place the query is executed:

DB::listen(function($sql, $bindings, $time) { 
    var_dump($sql);
    var_dump($bindings);
});

Posts::where('title', 'My First Post')->get();

die;

And it will echo it in the page.

You can also use the Clockwork Chrome extension (https://chrome.google.com/webstore/detail/clockwork/dmggabnehkmmfmdffgajcflpdjlnoemp?hl=en), this is the PHP package to be installed on Laravel: https://github.com/itsgoingd/clockwork.

Clockwork gives you a really nice profiling view of your application.

Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
  • But is there a way to log a specific query? That looks suspiciously global, and we'll have thousands running. Generally we'd want to see just the one on the Page Of Death. (Or preferably to have it echoed somewhere in the DEV server.) – Ferrett Steinmetz Jan 13 '14 at 21:27