DB::listen(function ($query) {
// $query->sql
// $query->bindings
// $query->time
});
from documentation https://laravel.com/docs/5.6/database#listening-for-query-events
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello everyone.
I'm having an issue here, I'm not being able to log queries besides selects to the database. I've tried toSql() which doesn't work with anything else but selects, and DB::getQueryLog() which retrieves an array, and I need the query as a string. Can anyone give me a little hand in here?
Thank you.
P.S: I'm on lumen 5.6.4
Not easily. What's wrong with DB::getQueryLog()? Yes it returns an array, but it has the same data (query/bindings/time) that DB::listen() gives you and you can run it in the controller. You can manually build up a "string" containing all 3 just like when logging it, except it's an array instead of an object.
Please or to participate in this conversation.