Skip to content

Instantly share code, notes, and snippets.

@davestevens
Created April 3, 2015 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davestevens/ae38c55d2a1c4e7437ca to your computer and use it in GitHub Desktop.
Save davestevens/ae38c55d2a1c4e7437ca to your computer and use it in GitHub Desktop.
Displays all Database Queries in Laravel 5
<?php namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesCommands;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Log;
use DB;
abstract class Controller extends BaseController {
use DispatchesCommands, ValidatesRequests;
public function __construct()
{
$this->beforeFilter('@enableQueryLogging');
$this->afterFilter('@outputQueryLogging');
}
public function enableQueryLogging($route, $request)
{
Log::info('Started ' . $request->method() . ' ' . $request->server('PATH_INFO'));
DB::connection()->enableQueryLog();
}
public function outputQueryLogging()
{
foreach (DB::getQueryLog() as $query)
{
$replacements = $query['bindings'];
$output = preg_replace_callback('/\?/', function($matches) use (&$replacements) {
return array_shift($replacements);
}, $query['query']);
Log::info('(' . $query['time'] . ') ' . $output);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment