Laravel-数据库监听
<?php
namespace App\Providers;
class AppServiceProvider extends ServiceProvider
{
public function boot(){
DB::listen(function($query){
//剔除命令行sql
if(app()->runningInConsole()) return;
$request = request();
//防止重复录入形成死循环 sql_log有关的表不显示
if(strpos($query->sql,'sql_log' !=false)) return;
$sqlLog = [
'sql' => vsprintf(str_replace("?", "'%s'", $query->sql), $query->bindings),
'time' => $query->time,
'url' => $query->url,
'fingerprint' => $request->fingerprint()
];
});
}
}