Every SQL query of a Laravel request with ray()
Point spatie/laravel-ray at Buggregator and call ray()->showQueries() before the code you want to watch. Each SQL query then shows up in Buggregator with its bindings filled in, the connection name and how long it took in milliseconds. It is a query log you read in the browser, not in a file.
Steps
1. Point Ray at Buggregator
Publish the Ray config, then set two lines in .env. In Docker Compose, use RAY_HOST=ray@buggregator.
php artisan ray:publish-configRAY_HOST=ray@127.0.0.1
RAY_PORT=80002. Show the queries of one piece of code
Pass a closure: Ray shows only the queries run inside it.
ray()->showQueries(function () {
User::with('orders')->where('active', true)->get();
});3. Or switch it on and off
Everything between the two calls is sent.
ray()->showQueries();
$users = User::where('active', true)->get();
ray()->stopShowingQueries();4. Or send every query
To see all queries of every request, set this in .env. It reads the published config/ray.php.
SEND_QUERIES_TO_RAY=true5. Count them or catch the slow ones
countQueries sends a small table with the number of queries and their total time. showSlowQueries sends only queries slower than the given milliseconds.
ray()->countQueries(function () {
User::all()->each(fn ($user) => $user->orders->count());
});
ray()->showSlowQueries(100);Open http://127.0.0.1:8000 to read them. Each query shows the file and line it came from.
Why it works
Laravel fires an event for every executed query. Laravel Ray listens to it while query watching is on and sends each query to the host and port you set. Buggregator takes Ray payloads on port 8000 and renders a query as SQL with highlighting, plus the connection and the time.
Related tips
- Dumps
Send ray() calls to a free local viewer
Set RAY_HOST and RAY_PORT in your Laravel .env. Your ray() calls, queries, jobs and mails show up in Buggregator, free and MIT licensed.
- Profiler
Find the slow function in a Laravel page
Install XHProf and one Laravel package, point it at Buggregator, and read the flamechart and top functions of every request.
- Dumps
dump() without breaking your page or JSON response
Send Symfony VarDumper output to Buggregator with two env vars. dump() stops landing in your HTML or JSON and shows the file and line it came from.
- Logs
Laravel logs from web, queue and cron in one live view
Add a socket channel to config/logging.php and set LOG_CHANNEL=socket. Laravel logs from requests, queue workers and cron jobs appear live in Buggregator.
Checked on with Buggregator v2.13.0