Created
June 1, 2016 14:43
-
-
Save 4lun/303e2bc69e5b40e00f5fbfce1841e314 to your computer and use it in GitHub Desktop.
Easily capture full SQL queries in Laravel (tested in 5.2), useful for quickly inspecting queries inside controllers and scoping certain blocks of code
This file contains 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 | |
\DB::enableQueryLog(); | |
// Code that triggers DB queries (User::create(), User::with('relation')->get(), etc) | |
$pdo = \DB::getPdo(); | |
$queries = collect(\DB::getQueryLog())->map(function($log) use ($pdo) { | |
$query = $log['query']; | |
foreach($log['bindings'] as $binding) { | |
$query = preg_replace('/\?/', $pdo->quote($binding), $query, 1); | |
} | |
return $query; | |
}); | |
dd($queries); // or \Log::debug($queries); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment