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

filipe1221's avatar

Query log

Does anyone know how to monitor the time of a query? I know there is a query log but my idea is guarded in bd whenever any query exceeds a certain time defined by me

0 likes
10 replies
Cronix's avatar
\DB::listen(function($query) {
    $queryTime = $query->time;
    $sql = $query->sql;
    $bindings = $query->bindings;
});
2 likes
shez1983's avatar

you can also enable slow query log on your server so it will log all slow queries.

1 like
filipe1221's avatar

$this->query_list=$this->model->select('id', 'title')->orderBy('item_order', 'asc')->get()->toArray();

How can i execute that here?

filipe1221's avatar

i just want create a function to send me a email and save in db every time wich a query was executed

Cronix's avatar
Cronix
Best Answer
Level 67

i just want create a function to send me a email and save in db every time wich a query was executed

Can you be more specific? For any query that runs? Or a specific query?

If you put this in your AppServiceProviders boot() method, it will monitor all queries. You can take an action if the query time took longer than your threshold.

$maxTime = 30;

\DB::listen(function($query) use ($maxTime) {

    if ($query->time > $maxTime) {
        // do something... log it, trigger an event, send an email, etc

        // log the query being run, the bindings (if any), and how long it took the query
        \Log::warning($query->sql, [
            'Bindings' => $query->bindings,
            'Time' => $query->time
        ]);
    }
});
1 like
Cronix's avatar

But iam using eloquent

Which uses DB. It will get all queries run, whether using DB facade or Eloquent.

filipe1221's avatar

but can i use this in ELOQUENT? Sorry iam learning :)

Please or to participate in this conversation.