Easy way to log all SQL queries in Laravel Application

profile picture

Easy way to log all SQL queries in Laravel Application

Every developer, when working on a project, start at some point wandering about the performances of his application. In the Laravel world, the most frequent question you may have is "are my sql queries fast enough ?". If Eloquent is doing a great job optimizing your queries, sometines it's not enough.

If you want to analyze the SQL queries of your application, behind Eloquent, you then need some tool to log all the executed queries. Laravel Debugbar is a great tool for that and it will help you lat.

For some projects however, you don't especially want to setup the Laravel Debugbar package. In this case, how can you log SQL queries ?

There is a simple snippet that you can add in the AppServiceProvider that will help you quite a lot.

In the boot() method:

\DB::listen(function($query) { \Log::info($query->sql, $query->bindings); });

Now, when running you application, every single query will be written down in the laravel.log file, located in storage/

I hope it'll help you as much as it helps me!

Cheers

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

    0x519e0eaa9bc83018bb306880548b79fc0794cd08

  • Monero

    XMR

    895bSneY4eoZjsr2hN2CAALkUrMExHEV5Pbg8TJb6ejnMLN7js1gLAXQySqbSbfzjWHQpQhQpvFtojbkdZQZmM9qCFz7BXU

recent posts

QBUS - MQTT Quick Tutorial

1
10 months ago

DDOS By Web Crawlers

1
1 year ago

Something Given

1
1 year ago

2024 © My Dynamic Production SRL All rights Reserved.