The Oddler
The Oddler

Reputation: 6718

Laravel 5.3 log query result?

I'm trying to find out which sql queries are slow in a legacy application of my company, so I'm logging the queries as described at https://laravel.com/docs/5.3/database#listening-for-query-events

DB::listen(function ($query) {
    \Log::info($query->sql);
    \Log::info($query->bindings);
    \Log::info($query->time); //in milliseconds
}

And this works fine. However, it would be interesting to log the actual result of the queries too. Is that possible?

I couldn't find what properties this query variable has, is it just those three? Or can I get the actual result of the query too?

This is the first time I work in php, so assume no knowledge. Thanks a lot!

Upvotes: 0

Views: 215

Answers (2)

Khetesh kumawat
Khetesh kumawat

Reputation: 711

You will need to enable the query log by calling. Beginning Of code

DB::enableQueryLog();

//Here is your models or Db query execute code.  

Then you can get query logs by simply:

End of the query.

  \Log::info(DB::getQueryLog());
  //dd(DB::getQueryLog());

Upvotes: 0

Jasper Helmich
Jasper Helmich

Reputation: 751

You could use the logger helper

https://laravel.com/docs/5.3/helpers#method-logger

logger()

The logger function can be used to write a debug level message to the log:

logger($query->get());

So where ever you build up your query get the data en logger($data);

Upvotes: 1

Related Questions