laravellaravel-5.8

Get number of returned rows from a query using DB::Listen()


I'm addding some database logging to a laravel (5.8) application and I have registered a DB::listener callback, but it seems I'm fairly limited to the data the $query object has populated.

It does have the time taken to execute, the statement, so it must be being logged after the query is run, so it would make sense for it to be posible to return the number of rows impacted/returned.

I've configured a custom channel for the DB logs, and only enabled them when a config value is set.

My implementation looks like the below.

    if (config('app.sql_profiler')) {
        DB::listen(function ($query) {
            Log::channel('db')->debug(
                $query->sql,
                [$query->bindings, $query->time]
            );
        });
    }

I would like to extend it to look like

    if (config('app.sql_profiler')) {
        DB::listen(function ($query) {
            Log::channel('db')->debug(
                $query->sql,
                [
                    $query->bindings,
                    $query->time,
                    // add $query->resultCount.
                ]
            );
        });
    }

Any suggestions as to where to begin looking would be very helpful.


Solution

  • No, this is not possible.

    You are limited to the data attributes described here.