Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Lumen - Eloquent Query Log

Tags:

laravel

lumen

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?

like image 782
ajtrichards Avatar asked Jul 29 '15 15:07

ajtrichards


2 Answers

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

like image 92
ajtrichards Avatar answered Oct 14 '22 14:10

ajtrichards


Just call this after the query to keep it quick and simple:

echo $query->toSql();
like image 26
Mahen Nakar Avatar answered Oct 14 '22 14:10

Mahen Nakar