Posts Learn Components Snippets Categories Tags Tools About

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

Learn the way to get the total number of queries by listening to any query that was run on Laravel

Created on Aug 06, 2021


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.

use \DB;

$count = 0;

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

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

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


PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that are right for your project.

View Components

Sponsors 👑

+ Add Yours