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

414 views

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

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

)