Showing posts with label getQueryLog. Show all posts
Showing posts with label getQueryLog. Show all posts

Saturday, February 11, 2017

How to get the query executed in Laravel

As a developer you must face some situation where you have to need the sql executed. In Laravel you can see your sql query generated each step. Below is a simple code snippet to output the sql query generated by Laravel query generator. Also you have the parameters there used in Laravel query.

DB::enableQueryLog();
$total = DB::table('user')
       ->where('group_id', '=', 1)
       ->where('status', '=', 1)
       ->count();
$queries = DB::getQueryLog();
$last_query = end($queries);
echo "<pre>"; print_r($last_query); echo "</pre>";


Output following:




Array
(
    [query] => select count(*) as aggregate from `user` where `group_id` = ? and `status` = ?
    [bindings] => Array
        (
            [0] => 64
            [1] => 1
        )

    [time] => 0
)