16

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.

Is there something like that in 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?

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

4

8 に答える 8

17

Laravel 4 を使用している場合は、これを使用します。

$queries    = DB::getQueryLog();
$last_query = end($queries);
于 2013-06-27T09:06:27.533 に答える
6

以下は、マスター ページ テンプレートに挿入できる簡単な Javascript スニペットです。含まれている限り、すべてのクエリがブラウザの Javascript コンソールに出力されます。それらを読みやすいリストに出力し、サイトを簡単に閲覧して、各ページで実行されているクエリを確認できます.

デバッグが完了したら、テンプレートから削除してください。

<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>
于 2013-09-30T02:12:40.520 に答える
5

There is a Composer package for that: 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 debug to true in your configuration.

于 2013-06-27T09:40:36.717 に答える
2

私は本当に簡単な方法を思いつきました(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");
});

しかし、より公式な解決策を見つけることを望んでいます。

これにより、次のような SQL ステートメントが出力されます。

[SQL] select 1 in 0.06s
于 2013-06-27T09:07:32.660 に答える
1

QueryBuilder インスタンスにはメソッドtoSql()があります。

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

戻ります:

select * from `employees`

これは、クエリを表示する最も簡単な方法です。

于 2016-06-07T08:28:53.870 に答える
1

このコードは他のソースから直接取得されたものですが、端末ウィンドウを使用してPHPStormで機能したので、簡単にしたかったのですが、完全なログを表示できましたが、ログイン後にSentryがありました。

1.追加

'log'=>true

あなたの中とあなたconfig/database.phpのデータベース名の場所の下ex.mysql

次に、以下のコードをroutes.php上記のすべてのルート構成の下に追加します。これは、特定のルート構成の下でそれを作成できるためですが、そのルートが呼び出されたときしか表示されません。

この出力を表示するには/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);
    });
}

ブレークポイントを作成することを忘れないでください....または私にpingしてください:)

于 2014-09-04T09:25:56.410 に答える