laravel打印查询日志

laravel在xdebug调试的时候不能像tp一样查看sql,比较麻烦,在网上查了一下资料,在此做个笔记: 1、使用socketLog来打印sql 到githuh下载socketLog文件,将php文件夹拷贝到项目根目录; 2、在app/Providers/EventServiceProvider.php中的$listen内加入 'Illuminate\Database\Events\QueryExecuted' => [ 'App\Listeners\QueryListener', ], php

php artisan event:generate生成监听器

添加内容 class QueryListener { /** * Create the event listener. * * @return void */ public function __construct() { // }

/**
 * Handle the event.
 *
 * [@param](https://my.oschina.net/u/2303379)  QueryExecuted  $event
 * [@return](https://my.oschina.net/u/556800) void
 */
public function handle(QueryExecuted $event)
{
    //
    if (env('APP_DEBUG', false)) {
        $slog = require_once __DIR__.'/../../php/slog.function.php';
        $sql = str_replace("?", "'%s'", $event->sql);
        $log = vsprintf($sql, $event->bindings);

// Log::info($log); slog($log); } } } 3、其它就是socketLog的配置了

猜你喜欢

转载自my.oschina.net/u/2618337/blog/1476515