Laravel 5 Eloquent: How to get raw sql that is being executed? (with binded data)

前端 未结 5 1637
栀梦
栀梦 2021-02-06 01:13

Im trying to figure out how to get the raw sql query being executed including the binded data in it. Here is what ive got:

\\DB::connection()->enableQueryLog(         


        
5条回答
  •  广开言路
    2021-02-06 01:22

    Add this function to your application and simply call.

    function getQuery($sql){
            $query = str_replace(array('?'), array('\'%s\''), $sql->toSql());
            $query = vsprintf($query, $sql->getBindings());     
            return $query;
    }
    $foo = Foo::where('bar', 'baz');
    print_r(getQuery($foo));
    

    Output: select * from Foo where bar = 'baz'

提交回复
热议问题