Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Eloquent ORM's getQueryLog() outside of Laravel?

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

like image 257
Martyn Avatar asked Jan 17 '15 10:01

Martyn


People also ask

Can I use eloquent outside Laravel?

If you want, you can use Eloquent without Laravel. Actually, Laravel is not a monolithic framework. It is made up of several, separate parts, which are combined together to build something greater. However, nothing prevents you from using only selected packages in another application.

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

DB::QueryLog() works only after you execute the query using $builder->get() . If you want to get the raw query before or without executing the query, you can use the $builder->toSql() method.

Which is better eloquent or query builder in Laravel?

When you are handling more data, it is better to use Laravel's DB facade query builder than Laravel's Eloquent ORM. From performance tests, inserting 1000 rows in a simple table takes Eloquent 1.2 seconds whereas the DB facade takes only 800 milliseconds.


2 Answers

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

like image 157
Buksy Avatar answered Oct 30 '22 09:10

Buksy


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);
}
like image 23
robbash Avatar answered Oct 30 '22 07:10

robbash