Created
April 3, 2015 11:46
-
-
Save davestevens/ae38c55d2a1c4e7437ca to your computer and use it in GitHub Desktop.
Displays all Database Queries in Laravel 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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