How To Log Eloquent Database Queries In Laravel

profile picture

How To Log Eloquent Database Queries In Laravel

Today I was trying to debug a big Eloquent Query in one of my Laravel Applications, and I couldn't understand what was the issue.

I wrote a feature test and hit the route endpoint to trigger the controller's code, to check the values returned by the Eloquent query that is inside, but the results were wrong.

The only way to debug this is to actually check what SQL query is executed by Eloquent.

There is a really easy way to do that, and it immediately helped me debug my Eloquent Query.

In my test I simply added those lines:

Just Before executing the test's code

DB::connection()->enableQueryLog();

Right after hitting the controller or the Eloquent Query

dd(DB::getQueryLog());

Run that specific test and you'll see exactly what is the SQL query that Eloquent is executing.

Hope it helps ;)

laravel
eloquent
log
query
sql

about me

profile picture

I consider myself as an IT Business Artisan. Or Consultant CTO. I'm a self-taught Web Developper, coach and teacher. My main work is helping and guiding digital startups.

more about me

follow me

newsletter

A weekly email with the latests articles

support my work

Start trading on binance
  • BTC

    BTC

    18SY81ejLGFuJ9KMWQu5zPrDGuR5rDiauM

  • ETH

    ETH

    0x68e8c828b8ce670062a83199cd8f62667c792f23

  • Monero

    XMR

    895bSneY4eoZjsr2hN2CAALkUrMExHEV5Pbg8TJb6ejnMLN7js1gLAXQySqbSbfzjWHQpQhQpvFtojbkdZQZmM9qCFz7BXU

2021 My Dynamic Production SPRL All rights Reserved.