Posts Learn Components Snippets Categories Tags 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

17 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

Snippets by

PostSrc poster

PostSrc

Full stack developer mainly focusing on the front end side of the web. The main author of PostSrc and loves writing tutorial and guides online.

new

PostSrc Code Components

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

View Components

Other Code Snippets

More