Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

domhnallw's avatar

Getting the Eloquent Query Log outside of Laravel...

Hi all:

Am trying to debug a weird situation where data is getting deleted unexpectedly, so am trying to get a log of the SQL that Eloquent generates in the course of its business. This forms part of an API layer I'm building for an older application; I've basically built Eloquent models that match the existing database structure and am using that to build out an API layer using the Slim Framework, The PHP League's Fractal and a bunch of other tools (this project was started before Lumen launched...)

So, basically, I'm booting Eloquent using the Capsule manager, basically as follows:

use Illuminate\Database\Capsule\Manager as Capsule;

$db = new Capsule();
$db->addConnection([
    'driver' => getenv('DATABASE_DRIVER'),
    'host' => getenv('DATABASE_HOST'),
    'database' => getenv('DATABASE_SCHEMA'),
    'username' => getenv('DATABASE_USERNAME'),
    'password' => getenv('DATABASE_PASSWORD'),
    'charset' => getenv('DATABASE_CHARSET'),
    'collation' => getenv('DATABASE_COLLATION'),
    'prefix' => getenv('DATABASE_PREFIX'),
]);

$db->setAsGlobal();
$db->bootEloquent();

What I'm trying to do is get a list of the SQL queries executed by Eloquent as part of a particular API call. I know that DB::getQueryLog() should work inside a Laravel instance. However, I can't seem to get this right. I've tried a couple of things, including (using the $db set above) $db->getConnection()->getQueryLog(), but I seem to be getting a big fat nada.

Any advice appreciated on this, it's driving me nuts...

0 likes
3 replies
thomaskim's avatar
Level 41

Did you enable the query log?

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

It's disabled by default now.

domhnallw's avatar

Yep, that did it. Thanks. Feel a bit silly now :(

LuyandaSiko's avatar

Hi guys, I have followed the same logic to get the queries executed within my models to display but in vein. I am on Lument 5.5.*... My collection always returns as empty and I am using the following packages for search functionality. The same code I implement is the same as the one I have on the main Laravel application just that somehow the mysql drive does not seem to cut it with Lumen

"laravel/scout": "^3.0",
"yab/laravel-scout-mysql-driver": "^2.0"

Please or to participate in this conversation.