1

I have a phpunit test that does some query stuff and then:

$results = $thingy::where("finder_id", "=", "37");
$queries = DB::getQueryLog();
dd($queries);

but $queries always returns an empty array

Config::get('app.debug') is true.

I have tried:

DB::enableQueryLog() 
DB::connection()->enableQueryLog()

with no luck. I have also tried various event listeners like:

Event::listen("illuminate.query", function($query, $bindings, $time, $name){
    \Log::sql($query."\n");
    \Log::sql(json_encode($bindings)."\n");
});

What else could be causing this?

EDIT: To eliminate PHPUnit as the cause, I setup a test route with a known working model

Route::get('fndr',function() {
    $customer = new Customer;
    $results = $customer->first();
    var_dump($results->NAME);
    $queries = DB::getQueryLog();
    dd($queries);

});

Still $queries is empty.

  • The logging takes place in the `Illuminate\Database\Connection` class. Within the `run` method of that class there is a call to the `logQuery` method. Try determining if the logging is actually being called and executed correctly. Your code is otherwise correct. – Bogdan Feb 27 '15 at 22:15

2 Answers2

2

This answer helped me:

[here]How to get the query executed in Laravel 5 ? DB::getQueryLog returning empty array

The Some Tips 1 Multiple DB connections If you have more than one DB connection you must specify which connection to log

To enables query log for my_connection:

DB::connection('my_connection')->enableQueryLog();

To get query log for my_connection:

print_r(
   DB::connection('my_connection')->getQueryLog()
);

Both enbleQueryLog() and getQueryLog() needed to specify the connection name

a_k_v
  • 1,558
  • 7
  • 18
Jack
  • 36
  • 2
1

It looks like I needed to specify the name of the connection to use. I'm assuming it uses the default connection otherwise.

$queries = DB::connection('name_of_connection')->getQueryLog();
dd($queries);