Laravel 5's built-in solution
In Laravel 5+, we can use \DB::getQueryLog()
to retrieve all executed queries. Since, query logging is an extensive operation and cause performance issues so it's disabled by default in L5 and only recommend for development environments only. We can enable the query logging by using the method \DB::enableQueryLog()
, as mentioned in [Laravel's documentation][1].
Problem in built-in solution
The DB::getQueryLog()
function is great but sometimes we wish that it would be great if we get dump in flat SQL format, so we can copy/past it in our favorite MySQL application like phpMyAdmin
or Sqlyog
to execute it and debug or optimize it.
So, I need a helper function that helps me to produce dump with following additional info:
- On which file and line number the dump has called.
- Remove back-ticks from the query.
- Flat query, so don't need to update binding parameters manually and I can copy/past
SQL
inphpMyAdmin
etc to debug/optimize the query.
back-ticks
, the generated SQL will be easy to read and debug, as the primary job of this function is to debug. Thanks forvsprintf
function, yes it will help me to reduce the code. – Hyehyena