php Laravel Eloquent 显示查询日志

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41140975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:20:13  来源:igfitidea点击:

Laravel Eloquent display query log

phplaravel

提问by Benjamin W

use App\Order;

public function show(Order $order){

        $data = $order->all();
        return dd($order->getQueryLog());

Is there any way to display the query built by Eloquent in Laravel?

有没有办法在 Laravel 中显示 Eloquent 构建的查询?

I tried getQueryLog();but its not working

我试过了,getQueryLog();但它不工作

回答by kapil.dev

First you have to enable query log it can be done using

首先,您必须启用查询日志,可以使用

DB::connection()->enableQueryLog();

then you can use below code to see the query log

然后你可以使用下面的代码来查看查询日志

$queries = DB::getQueryLog();

if you want to see the last executed query

如果您想查看上次执行的查询

$last_query = end($queries);

to know more about logging see this https://laravel.com/docs/5.0/database#query-logging

要了解有关日志记录的更多信息,请参阅此https://laravel.com/docs/5.0/database#query-logging

Example

例子

public function show(Order $order){
        \DB::connection()->enableQueryLog();
        $data = $order->all();
        $queries = \DB::getQueryLog();
        return dd($queries);
}

回答by Alexey Mezenin

To use getQueryLog()you need to enable it first:

要使用,getQueryLog()您需要先启用它:

DB::enableQueryLog();
DB::getQueryLog();

If you want to see real queries, you can use Laravel Debugbar, it will show all real queries Laravel created during current request.

如果你想看到真实的查询,你可以使用Laravel Debugbar,它会显示 Laravel 在当前请求期间创建的所有真实查询。

Sometimes ->toSql()is also useful.

有时->toSql()也很有用。

回答by markdwhite

Working on 5.6, something like this in AppServiceProvider::boot()

在 5.6 上工作,类似这样的 AppServiceProvider::boot()

    // Log all DB SELECT statements
    // @codeCoverageIgnoreStart
    if (!app()->environment('testing') && config('app.log_sql')) {
        DB::listen(function ($query) {
            if (preg_match('/^select/', $query->sql)) {
                Log::info('sql: ' .  $query->sql);
                // Also available are $query->bindings and $query->time.
            }
        });
    }

Then in config/app.php, just so it's easy to enable/disable from amending the .env

然后在 config/app.php 中,很容易启用/禁用修改 .env

    'log_sql' => env('LOG_SQL'),

All credit to: https://arjunphp.com/laravel-5-5-log-eloquent-queries/

全部归功于:https: //arjunphp.com/laravel-5-5-log-eloquent-queries/

And this can be parsed for unique queries with:

这可以解析为唯一查询:

    grep ") sql:" laravel.log | sed -e "s#.*select\(.*\)\[\]#select#" | sort -u

回答by Muhammad

To see the query logs in laravel.logfile use the following way.

要查看laravel.log文件中的查询日志,请使用以下方式。

namespace App\Providers;

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

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        DB::listen(function($query) {
            Log::info(
                $query->sql,
                $query->bindings,
                $query->time
            );
        });
    }

    // ...
}

回答by Juliano

I know it's an old question, but it might help others that had the same issue I had.

我知道这是一个老问题,但它可能会帮助其他有同样问题的人。

If you use other connection than the default one, you should specify it to get the query log properly.

如果您使用默认连接以外的其他连接,您应该指定它以正确获取查询日志。

\DB::connection('YourConnection')->enableQueryLog();
$test = MyModel::all();
$queries = \DB::connection('YourConnection')->getQueryLog();
dd($queries);

回答by Elisha Senoo

You can use ::toSql()or ->toSql()as demonstrated below:

您可以使用::toSql()->toSql()如下所示:

use App\Order;

public function show(Order $order){

    return $order::toSql();

Or

或者

use App\Order;

public function show(Order $order){

    return $order::where("id", "<>", 0)->toSql();

You might have to enable query log:

您可能必须启用查询日志:

DB::enableQueryLog();