Log last executed Eloquent query as plain SQL in Laravel 5

Published on Aug 31, 2015

Here's the small code snippet needed to quickly log the SQL output for your last executed Eloqent query in Laravel 5. I've had to look this up so many times, that I decided to just post it here for my own convenience. If you forget to enable the query log, you'll notice that it will be empty all the time.

\DB::connection()->enableQueryLog();
$query = \DB::getQueryLog();
$lastQuery = end($query);
No comments? But that’s like a Gin & Tonic without the ice?

I’ve removed the comments but you can shoot me a message on LinkedIn to keep the conversation going.