1、在app\Providers\EventServiceProvider.php中添加一个触发
<pre name="code" class="html">protected $listen = [
'App\Events\SomeEvent' => [
'App\Listeners\EventListener',
],
'Illuminate\Database\Events\QueryExecuted' => [
'App\Listeners\QueryListener'
]
];
然后在listenrs文件夹中创建一个文件app\Listeners\QueryListener.phpclass QueryListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param QueryExecuted $event
* @return void
*/
public function handle(QueryExecuted $event)
{
//
$sql = str_replace("?", "'%s'", $event->sql);
$log = vsprintf($sql, $event->bindings);
Log::info($log);
}
}
然后你就能到
storage\logs\laravel.log 查询你的sql了,优化sql合查询bug有很大的帮助。
本文介绍如何在Laravel应用中记录SQL查询日志,通过监听数据库查询事件并使用自定义监听器打印查询语句及参数,便于进行SQL优化与查询错误定位。
3605

被折叠的 条评论
为什么被折叠?



