Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of queries in each page load of Laravel 5

I tried to achive this by listening for query events but I don't seem to have any idea of how I got the count. This is only retrieving list of queries as the callback function cannot modified to return a count variable:

DB::listen(function ($query) {
    var_dump('<pre>'.$query->sql.</pre>);
});

I need a solution to count each page executed queries by Laravel.

like image 570
ClearBoth Avatar asked Aug 03 '16 23:08

ClearBoth


1 Answers

Super old question but when I searched for a way to do this here is where I ended up. Doesn't seem to be a built-in way to get the count but simply doing something like this will get you what you need;

$count = 0;
DB::listen(function ($query) use (&$count) {
    $count++;
});

You might want to store the count somewhere more suitable but this will give you and any others coming here from Google the general idea.

like image 181
hank Avatar answered Oct 22 '22 21:10

hank