Inspect your Laravel database queries

4 years ago

mpociot

The Tinkerwell table view allows you to easily inspect all performed queries. This snippet collects all your executed queries and then shows them in a table, which you can then sort and search.

Code

$queries = [];
// Set up database listener
DB::listen(function ($query) use (&$queries) {
  $queryData = (array)$query;
  unset($queryData['connection']);
  $queries[] = $queryData;
});

// Perform your queries
$course = Course::first();
$chapters = $course->chapters()->get();
foreach ($chapters as $chapter) {
  $chapter->videos;
}

// Return the queries to inspect them in the table view
return $queries;
New course: Desktop apps with Electron