<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //把SQL输出到日志中
        if ($this->app->environment('local')) { // 仅在本地环境启用
            DB::listen(function ($query) {
                // 格式化 SQL 语句
                $sql = $query->sql;
                foreach ($query->bindings as $binding) {
                    $sql = preg_replace('/\?/', "'" . addslashes($binding) . "'", $sql, 1);
                }
                // 输出到控制台
                //dump($sql . ' [' . $query->time . 'ms]');
                //输出到log
                Log::info($sql . ' [' . $query->time . 'ms]');
            });
        }
    }
}
