Why not just use debugbar or clockwork? Both shows it automatically
Mar 4, 2022
4
Level 24
Have enableQueryLog show me where the query exists.
So I am using enableQueryLog and it is showing me the queries that are being executed and how long they take. However I am wondering if there is a way to see the class and line number that executes said query. How do I see this information.
Side note, I am using the illuminate capsule/manager to grab this info so I can use querybuilder and eloquent in a non Laravel application.
class DatabaseConnection
{
use CallStatically;
public static function boot()
{
$capsule = new Manager();
foreach(self::connectionSettings() as $name => $settings) {
$capsule->addConnection($settings, $name);
}
$capsule->getConnection()->enableQueryLog();
$capsule->setAsGlobal();
$capsule->getConnection()->setEventDispatcher(new Dispatcher());
$capsule->getConnection()->listen(fn ($query) => self::logQueries($query));
$capsule->bootEloquent();
}
private function connectionSettings(): array
{
global $settings;
return [
"default" => [
"driver" => "mysql",
"host" => $settings['DB_HOST'],
"database" => $settings['DB_NAME'],
"username" => $settings['DB_USERNAME'],
"password" => $settings['DB_PASSWORD']
]
];
}
public function logQueries(QueryExecuted $query)
{
dump($query);
// $query->sql
// $query->bindings
// $query->time
}
}
Level 56
I think debug_backtrace() would give you this info:
https://www.php.net/manual/en/function.debug-backtrace.php
Not sure, but worth a try
Please or to participate in this conversation.