Laravel 4 - logging SQL queries

前端 未结 6 1698
时光取名叫无心
时光取名叫无心 2020-11-28 04:40

There are already several questions in regards to logging the SQL query in Laravel 4. But I\'ve tried almost all of them and it\'s still not working the way I want.

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 05:06

    You should be able to find the bindings by passing $bindings as the second parameter of the Event function.

    Event::listen('illuminate.query', function($sql, $bindings, $time){
        echo $sql;          // select * from my_table where id=? 
        print_r($bindings); // Array ( [0] => 4 )
        echo $time;         // 0.58 
    
        // To get the full sql query with bindings inserted
        $sql = str_replace(array('%', '?'), array('%%', '%s'), $sql);
        $full_sql = vsprintf($sql, $bindings);
    });
    

    In Laravel 3.x I think the event listener was called laravel.query

提交回复
热议问题