Laravel - 无法转储查询结果

时间:2017-12-06 06:54:29

标签: php laravel

我希望得到查询结果,就像这个example一样。

我试过给出的例子,但我没有得到任何输出。我可以知道我做错了吗?

以下是示例:

AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Validator;
use DB;
use Event;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        if (env('APP_ENV') === 'local') {
            DB::connection()->enableQueryLog();
            Event::listen('kernel.handled', function ($request, $response) {
                if ($request->has('sql-debug')) {
                    $queries = DB::getQueryLog();
                    dd($queries);
                }
            });
        }

        Schema::defaultStringLength(191);
    }
}

SeasonRecord.php

public function index()
{
    $cars = User::where('id', 3)
    ->get();

    // return view('seasonRecord', compact('cars'));
}

Web.php

Route::get('/record', 'SeasonRecord@index');

在我的.env文件中,APP_ENV设置为local

对于我写的http://127.0.0.1:8000/record?sql-debug=1网址,但没有显示结果。

1 个答案:

答案 0 :(得分:2)

Laravel版本5.4中不再有kernel.handledChange set

请尝试以下代码

...
Event::listen(\Illuminate\Foundation\Http\Events\RequestHandled::class, function ($event) {                         
    if ($event->request->has('sql-debug')) {    
        $queries = DB::getQueryLog();
        dd($queries);
    }
});
...