You can display all queries that are executed by calling showQueries (or queries).
peek()->showQueries();
// This query will be displayed in peek.
User::firstWhere('email', 'john@example.com');
To stop showing queries, call stopShowingQueries.
peek()->showQueries();
// This query will be displayed.
User::firstWhere('email', 'john@example.com');
peek()->stopShowingQueries();
// This query won't be displayed.
User::firstWhere('email', 'jane@example.com');
Alternatively, you can pass a callable to showQueries. Only the queries performed inside that callable will be displayed in Peek. If you include a return type in the callable, the return value will also be returned.
// This query won't be displayed
User::all();
peek()->showQueries(function() {
// This query will be displayed.
User::all();
});
$users = peek()->showQueries(function (): \Illuminate\Database\Eloquent\Collection {
// This query will be displayed and the collection will be returned.
return User::all();
});
User::all(); // this query won't be displayed.
If you're interested in how many queries a given piece of code executes, and what the runtime of those queries is, you can use countQueries. It expects you to pass a closure in which all the executed queries will be counted.
Similar to showQueries, you can also add a return type to your closure to return the result of the closure.
You can display all queries that took longer than a specified number of milliseconds to execute by calling showSlowQueries.
peek()->showSlowQueries(100);
// This query will only be displayed in Ray if it takes longer than 100ms to execute.
User::firstWhere('email', 'john@example.com');
Alternatively, you can also pass a callable to showSlowQueries. Only the slow queries performed inside that callable will be displayed in Ray.
User::all();
// This query won't be displayed.
User::all();
peek()->showSlowQueries(100, function() {
// This query will be displayed if it takes longer than 100ms.
User::where('id', 1)->get('id');
});
You can also use the shorthand method, slowQueries() which is the equivalent of calling showSlowQueries:
peek()->slowQueries();
To stop showing slow queries, call stopShowingSlowQueries.