Laravel Lumen - Eloquent Query Log

ajtrichards picture ajtrichards · Jul 29, 2015 · Viewed 9.3k times · Source

I'm using Laravel Lumen to build an API.

I've come to a point where I need to find out what SQL query is being generated by Eloquent. I know how to do this in Laravel 4 and Laravel 5 but i've tried the same code in Lumen and the query is blank?

$queries    = DB::getQueryLog();
$last_query = end($queries);

echo 'Query<pre>';
    print_r($last_query);
exit;

The above code, when run in Laravel works fine - in Lumen the query is blank?

Answer

ajtrichards picture ajtrichards · Jul 29, 2015

To get the query log in Laravel Lumen working you need to enable it:

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

You can add that code in to your controller, middleware, etc then use:

$queries    = DB::getQueryLog();
$lastQuery = end($queries);

dd($lastQuery)

To print your query.

You can also use the following with eloquent:

$myModel = Users::where('active', true);

dd($myModel->getSql(), $myModel->getBindings());

You must run the getSql() and getBindings() before you call ->first() or ->get(), etc