Laravel 4 SQL 日志/控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17338944/
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-09-14 08:02:09  来源:igfitidea点击:

Laravel 4 SQL log / console

laravellaravel-4

提问by Slava V

Is there something similar in Laravel that allows you to see the actual SQL being executed? In Rails, for example, you can see the SQL in console. In Django you have a toolbar.

Laravel 中是否有类似的东西可以让您看到正在执行的实际 SQL?例如,在 Rails 中,您可以在控制台中看到 SQL。在 Django 中,您有一个工具栏。

Is there something like that in Laravel 4?

Laravel 4 中有类似的东西吗?

To clarify: My question is how to do it without code. Is there something that is built-in in Laravel that does not require me to write code in app?

澄清:我的问题是如何在没有代码的情况下做到这一点。Laravel 中是否有内置的东西不需要我在应用程序中编写代码?

UPDATE: Preferably I'd like to see CLI queries as well (for example php artisan migrate)

更新:我最好也希望看到 CLI 查询(例如php artisan migrate

回答by Maeh

If you are using Laravel 4, use this:

如果您使用的是 Laravel 4,请使用以下命令:

$queries    = DB::getQueryLog();
$last_query = end($queries);

回答by Ashit Vora

I do this in Laravel 4.

我在 Laravel 4 中这样做。

Just set it once in app/start/global.phpor anywhere but make sure it is loaded and then it will start logging all your SQL queries.

只需在其中app/start/global.php或任何地方设置一次,但确保它已加载,然后它将开始记录您的所有 SQL 查询。

Event::listen("illuminate.query", function($query, $bindings, $time, $name){
    \Log::sql($query."\n");
    \Log::sql(json_encode($bindings)."\n");
});

回答by Adam

Here is a quick Javascript snippet you can throw onto your master page template. As long as it's included, all queries will be output to your browser's Javascript Console. It prints them in an easily readable list, making it simple to browse around your site and see what queries are executing on each page.

这是一个快速的 Javascript 片段,您可以将其放入母版页模板。只要包含它,所有查询都会输出到您浏览器的 Javascript 控制台。它将它们打印在一个易于阅读的列表中,从而可以轻松浏览您的站点并查看每个页面上正在执行的查询。

When you're done debugging, just remove it from your template.

完成调试后,只需将其从模板中删除即可。

<script type="text/javascript">
    var queries = {{ json_encode(DB::getQueryLog()) }};
    console.log('/****************************** Database Queries ******************************/');
    console.log(' ');
    $.each(queries, function(id, query) {
        console.log('   ' + query.time + ' | ' + query.query + ' | ' + query.bindings[0]);
    });
    console.log(' ');
    console.log('/****************************** End Queries ***********************************/');
</script>

回答by Niklas Modess

There is a Composer package for that: https://packagist.org/packages/loic-sharma/profiler

有一个 Composer 包:https: //packagist.org/packages/loic-sharma/profiler

It will give you a toolbar at the bottom with SQL queries, log messages, etc. Make sure you set debugto true in your configuration.

它会在底部为您提供一个带有 SQL 查询、日志消息等的工具栏。确保debug在您的配置中设置为 true。

回答by Antonio Carlos Ribeiro

Here's another nice debugging option for Laravel 4:

这是 Laravel 4 的另一个不错的调试选项:

https://github.com/barryvdh/laravel-debugbar

https://github.com/barryvdh/laravel-debugbar

回答by Daniel Adenew

This code is directly taken form other source but i wanted to make it easy for you as follow it worked for me on PHPStormusing my terminal window i was able to see a complete log but ,after loginthere was some Sentrything.

这段代码是直接从其他来源获取的,但我想让你很容易,因为它在PHPStorm 上使用我的终端窗口对我有用,我能够看到一个完整的日志,但是,登录后有一些Sentry 的东西。

1.add

1.添加

'log'=>true

'log'=>true

inside your config/database.phpand below the place ur database name ex.mysql

在您的内部config/database.php和您的数据库名称下方ex.mysql

then add below code toroutes.phpabove all no under any route configuration , since u can make that under a give route configuration but , u only see when that route is called.

然后将下面的代码添加到routes.php最重要的是在任何路由配置下都没有,因为您可以在给定的路由配置下进行,但是,您只能看到该路由何时被调用。

to see this output /goto / app/storage/log/somelogfile.log

查看此输出 /goto / app/storage/log/somelogfile.log

if (Config::get('database.log', false))
{
    Event::listen('illuminate.query', function($query, $bindings, $time, $name)
    {
        $data = compact('bindings', 'time', 'name');

        // Format binding data for sql insertion
        foreach ($bindings as $i => $binding)
        {
            if ($binding instanceof \DateTime)
            {
                $bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
            }
            else if (is_string($binding))
            {
                $bindings[$i] = "'$binding'";
            }
        }

        // Insert bindings into query
        $query = str_replace(array('%', '?'), array('%%', '%s'), $query);
        $query = vsprintf($query, $bindings);

        Log::info($query, $data);
    });
}

Dont forget to make break point .... or ping me :)

不要忘记制作断点....或ping我:)

回答by CelinVeronicca

In QueryBuilder instance there is a method toSql().

在 QueryBuilder 实例中有一个方法toSql()

echo DB::table('employees')->toSql()

echo DB::table('employees')->toSql()

would return:

会返回:

select * from `employees`

This is the easiest method to shows the queries.

这是显示查询的最简单方法。

回答by Slava V

I came up with a really simple way (if you are using php artisan serveand PHP 5.4) - add this to app/start/local.php:

我想出了一个非常简单的方法(如果您使用的是php artisan servePHP 5.4) - 将其添加到app/start/local.php

DB::listen(function($sql, $bindings, $time)
{
    file_put_contents('php://stderr', "[SQL] {$sql} in {$time} s\n" . 
                      "      bindinds: ".json_encode($bindings)."\n");
});

but hoping to find a more official solution.

但希望找到更官方的解决方案。

This will print SQL statements like this:

这将打印如下 SQL 语句:

[SQL] select 1 in 0.06s