首页 » 工作 » 方法 » 正文

记录 Laravel 的 SQL 记录

发布者:站点默认
2022/05/7 浏览数(37) 分类:方法 记录 Laravel 的 SQL 记录已关闭评论
# app/Providers/AppServiceProvider.php
public function boot()
{
    DB::listen(function ($query) {
        $location = collect(debug_backtrace())->filter(function ($trace) {
            return !str_contains($trace['file'], 'vendor/');
        })->first(); // grab the first element of non vendor/ calls

        $bindings = implode(", ", $query->bindings); // format the bindings as string

        Log::info("
            ------------
            Sql: $query->sql
            Bindings: $bindings
            Time: $query->time
            File: ${location['file']}
            Line: ${location['line']}
            ------------
        ");
    });
}
[2022-02-03 02:20:14] local.INFO:
------------
Sql: select "title", "slug", "body" from "posts" where "published" = ? order by "id" desc   
Bindings: 1
Time: 0.18
File: /home/cosme/Documents/projects/cosme.dev/app/Models/Post.php
Line: 61
----------

via: https://learnku.com/laravel/t/65164

点击返回顶部
  1. 留言
  2. 联系方式