Skip to content

Instantly share code, notes, and snippets.

@4lun
Created June 1, 2016 14:43
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 4lun/303e2bc69e5b40e00f5fbfce1841e314 to your computer and use it in GitHub Desktop.
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
<?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