Laravel记录sql日志

1.使用DB的listen方法

文件位置:/app/Providers/AppServiceProvider.php

use Illuminate\Support\Facades\DB;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        DB::listen(function ($query) {
            $sql = $query->sql;
            $bindings = $query->bindings;
            //写入sql
            if ($bindings) {
                file_put_contents('.sqls', "[" . date("Y-m-d H:i:s") . "]" . $sql . "\r\nparmars:" . json_encode($bindings, 320) . "\r\n\r\n", FILE_APPEND);
            } else {
                file_put_contents('.sqls', "[" . date("Y-m-d H:i:s") . "]" . $sql . "\r\n\r\n", FILE_APPEND);
            }
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

效果如下(文件在/public/下)

[2018-12-18 11:38:56]select * from `teacher_user` where `phone` = ? limit 1
parmars:["15210824753"]

[2018-12-18 11:38:56]select * from `teacher_user` where (`phone` = ? and `password` = ?) limit 1
parmars:["15210824753","e10adc3949ba59abbe56e057f20f883e"]

[2018-12-18 11:38:56]select * from `teacher_jurisdiction`

[2018-12-18 11:38:56]select `name` from `teacher_diurnal_knot_project` where (`is_delete` = ? and `school_id` = ?)
parmars:[2,"5fbfbb709c6711e89d1a95298076bdea"]

[2018-12-18 11:38:56]update `teacher_user` set `time_out` = ?, `update_time` = ? where (`token` = ?)
parmars:[1545107936,1545104336,"9d043e0d25fa78e349a0ace32bb13145184428e1"]

 

你可能感兴趣的:(php,框架,Laravel,laravel,记录,Sql,Laravel记录SQL)