Laravel 4 SQL log / console

后端 未结 8 2137
感动是毒
感动是毒 2020-12-31 05:50

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 tool

相关标签:
8条回答
  • 2020-12-31 05:58

    This code is directly taken form other source but i wanted to make it easy for you as follow it worked for me on PHPStorm using my terminal window i was able to see a complete log but ,after login there was some Sentry thing.

    1.add

    'log'=>true

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

    then add below code toroutes.php above 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.

    to see this output /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 :)

    0 讨论(0)
  • 2020-12-31 05:59

    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.

    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>
    
    0 讨论(0)
  • 2020-12-31 06:00

    In QueryBuilder instance there is a method toSql().

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

    would return:

    select * from `employees`
    

    This is the easiest method to shows the queries.

    0 讨论(0)
  • 2020-12-31 06:05

    I came up with a really simple way (if you are using php artisan serve and PHP 5.4) - add this to 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] select 1 in 0.06s
    
    0 讨论(0)
  • 2020-12-31 06:12

    If you are using Laravel 4, use this:

    $queries    = DB::getQueryLog();
    $last_query = end($queries);
    
    0 讨论(0)
  • 2020-12-31 06:15

    Here's another nice debugging option for Laravel 4:

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

    0 讨论(0)
提交回复
热议问题