Skip to content

Instantly share code, notes, and snippets.

@Mulkave
Created April 3, 2014 14:49
Show Gist options
  • Save Mulkave/9955756 to your computer and use it in GitHub Desktop.
Save Mulkave/9955756 to your computer and use it in GitHub Desktop.
Log Queries in Laravel 4
<?php
/*
should be in your routes/filters or anywhere else
they can be read on boot..
*/
Event::listen('illuminate.query', function($query, $bindings, $time, $name)
{
$data = compact('bindings', 'time', 'name');
// format binding data for 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);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment