Using a new connection for logging the transactions will do the trick.. use the mysqli_connect to define the connection.. get the parameters required for that transaction using env('DB_DATABASE').. and so on..
Save query on db listen event
I have a requirement that I need to save each and every query executed by the laravel program. I have created following listener in boot method of app/Http/Providers/AppServiceProvider
public function boot()
{
DB::listen(
function ($query) {
$query_binding = '';
foreach ($query->bindings as $binding) {
$query_binding .= $binding . ', ';
}
QueryLog::create([
'sql' => $query->sql,
'bindings' => $query_binding,
'time' => $query->time
]);
}
);
}
I get following error:
SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'bindings' at row 1 (SQL: insert into query_log (sql, bindings, time, updated_at, created_at) values (insert into query_log (sql, bindings, time, updated_at, created_at) values (insert into query_log (sql, bindings, time, updated_at, created_at) values (0.17, 2018-03-26 07:27:36, 2018-03-26 07:27:36, ?, ?), insert into query_log (sql, bindings, time, updated_at, created_at) values (?, ?, ?, ?, ?), select * from users where id = ? limit 1, 3, , 2.63, 2018-03-26 07:27:36, 2018-03-26 07:27:36, , 0.23, 2018-03-26 07:27:36, 2018-03-26 07:27:36, , 0.17, 2018-03-26 07:27:36, 2018-03-26 07:27:36, , ?, ?, ?, ?), ?, ?, ?, ?))
I believe this is due to the DB::listen listen to its own query and try to save it creates an infinite loop.
How to exclude the saving query of DB::listen and save all the other queries?
Please or to participate in this conversation.