Home / Snippets / How to Count The Number of Queries to the Database in Laravel
How to Count The Number of Queries to the Database in Laravel cover

How to Count The Number of Queries to the Database in Laravel

1.2K

3 years ago

0 comments

To know how many times your Laravel query is hitting the database, you can make use of the "listen()" method from the "DB" facade.

Below is the code example of how you can listen to the "DB" facade.
<?php

use \DB;

$count = 0;

DB::listen(fn ($sql) => $count++);

dd($count); // get the total count
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this