如何使用Laravel / Laragon调试/查看SQL请求

时间:2019-04-23 07:04:26

标签: laravel laravel-5 laragon

我的问题很简单,但是我不知道:我一直一直在使用java和eclipse或C#进行开发,因此这是一个“新世界”:)

如何调试laravel代码?

我正在使用laravel和vuejs。我从laragon启动服务器,然后使用VScode。我尝试从VScode(在laragon中添加了xdebug dll)启动它,但它没有给我更多信息,而且我无法使用断点来调试任何东西:

{
    "name": "Launch localhost",
    "type": "chrome",
    "request": "launch",
    "url": "http://127.0.0.1/",
    "webRoot": "C://laragon//www//projet"
},
{
    "name": "Launch index.php",
    "type": "chrome",
    "request": "launch",
    "file": "C://laragon//www//projet//index.php"
},

当我遇到sql错误时,我得到的只是

app.js:651 Uncaught (in promise) Error: Request failed with status code 500
    at createError (app.js:651)
    at settle (app.js:814)
    at XMLHttpRequest.handleLoad (app.js:184)

如何查看我的SQL请求并获得“实际错误”? 以及如何正确调试?

非常感谢。

2 个答案:

答案 0 :(得分:2)

您有多种选择来实现SQL日志记录:

1。通过在服务提供者中注册查询侦听器来使用查询侦听器(引导方法如下)

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use DB;
use Log;


class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Schema::defaultStringLength(191);

        DB::listen(function($query) {
            Log::info(
                $query->sql,
                $query->bindings,
                $query->time
            );
        });
    }
}
  1. 使用查询日志记录,请参阅https://laravel.com/docs/5.0/database#query-logging

  2. 使用laravel调试程序包,请参阅https://laravel.com/docs/5.0/database#query-logging

您可以使用xdebug调试php,但我仍然希望以上3种选项中的任何一种。

答案 1 :(得分:0)

您可以使用Laravel Debugbar查看实时查询。它提供了许多其他有用的信息以进行调试。请检查此链接https://github.com/barryvdh/laravel-debugbar

还尝试通过在服务提供商内部注册集合,使用查询侦听器将集合登录到SQL:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Log;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        DB::listen(function ($query) {

            // $query->sql
            // $query->bindings
            // $query->time

            Log::useDailyFiles(storage_path().'/logs/query_log.log');
            $query = $query->sql;
            Log::info($query);
        });
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}