1

I know you can log queries in Laravel. But I'm wondering if there's a way to also log the name of the script that ran the query. So for instance, if an update query was logged, I'd like to also know that it was initiated by PostController.php.

I'm working on a very large application that uses hundreds of queries, and when I need to remove one, it's very difficult to locate where that query is coming from.

Any ideas?

1
  • I think of make an syntax error with my SQL, to identify the caller from exception stack, kind of debugging Commented Mar 3, 2018 at 0:03

1 Answer 1

2

As you can see in Connection::logQuery(), each database query triggers a QueryExecuted event.

You could listen to this event, and use debug_backtrace() to crawl up the function calls.

use Illuminate\Support\Facades\DB;

DB::listen(function ($query) {

    // send this to a log or whatever
    $backtrace = debug_backtrace();

    // also, you can read $query to get the SQL string, bindings...
});

The main work will probably be to filter the data, to isolate what you are looking for.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.