Log last executed Eloquent query as plain SQL in Laravel 5

Joris Hens

Aug 31, 2015 · 1 min read

Hi! I'm GoodBytes. Web developer, teacher and souschef.

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);
« Stop using perfect pictures: meaningful, realistic design is more important
Keep on reading
Getting started with web design and development? AMA! »