6

I've been trying to figure out a way to log SQL queries from Eloquent ORM which I'm using within Zend Framework 1. I came across getQueryLog() method called in this manner:

$queries = DB::getQueryLog();

I found Illuminate\Database\Connection to contain the getQueryLog() method so I tried to do the following:

use Illuminate\Database\Connection as DB;

class IndexController
{
    .
    .
    .
    public function indexAction()
    {
        // do stuff (e.g. fetch/update/create rows) 
        $questions = Questions::all()
        .
        .
        $queries = DB::getQueryLog();
        var_dump($queries); exit;
        .
        // render view
    }
}

However, I get the following notice, and it returns NULL: Notice: Undefined property: IndexController::$queryLog in /var/www/qasystem/vendor/illuminate/database/Illuminate/Database/Connection.php on line 918 NULL

Can someone please suggest how I might use this outside of Laravel? I've searched online and can't see anything that I need to do different, although I suspect most examples will be used within Laravel. Also, is Illuminate\Database\Connection the correct class? Thanks

Martyn
  • 6,031
  • 12
  • 55
  • 121
  • Does this work `Capsule::getQueryLog()`? – lukasgeiter Jan 17 '15 at 11:03
  • Which version of laravel you are using ? – Eimantas Gabrielius Jan 17 '15 at 11:06
  • `Capsule::getQueryLog()` doesn't work. I'm not using Laravel, I'm using Eloquent within Zend Framework (1). – Martyn Jan 17 '15 at 14:52
  • Does `Questions::getConnection()->getQueryLog()` work? – patricus Jan 18 '15 at 08:47
  • Did you got anything working? trying to find the same! – jtanmay Aug 12 '15 at 14:12
  • Hi @jtanmay, I think I ended up using PHP Debug Bar which is a really neat tool for viewing inline SQL queries and other debug stuff (similar to browser inspectors). Anyway I think for that I just required the PDO instance that is used in Eloquent which I obtained by `$conn = $anyEloquentModelObject->getConnection(); $pdo = $conn->getPdo();`. I guess from PDO object, somehow, you'll be able to get the SQL queries executed. Actually, looking at the previous comments, perhaps $conn->getQueryLog() will work when $conn is obtained this way. Hope that helps. – Martyn Aug 13 '15 at 02:13

2 Answers2

2

Use toSql method instead. It will return you the final query command.

See How do I get the query builder to output its raw SQL query as a string? for more info

Community
  • 1
  • 1
Buksy
  • 11,571
  • 9
  • 62
  • 69
1

Even though a bit older- I just had the same issue, and using toSql() wasn't helping me as I have many-to-many relations and Eloquent executed more queries.

Based on @patricus' comment I got it working like this:

function getTheThing() {
  (new Thing())->getConnection()->enableQueryLog();
  $thing = Thing::whereUid('something')
    ->with('AnotherThing')
    ->first();
  $loggedSqls = (new Thing())->getConnection()->getQueryLog();
  var_dump($loggedSqls);
}
robbash
  • 985
  • 10
  • 21