Skip to main content

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.

bash
php artisan ray:publish-config
dotenv
RAY_HOST=ray@127.0.0.1
RAY_PORT=8000

2. Show the queries of one piece of code

Pass a closure: Ray shows only the queries run inside it.

php
ray()->showQueries(function () {
    User::with('orders')->where('active', true)->get();
});

3. Or switch it on and off

Everything between the two calls is sent.

php
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.

dotenv
SEND_QUERIES_TO_RAY=true

5. 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.

php
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.

Checked on with Buggregator v2.13.0

Setup in 60 seconds

No registration. No account. Start it and point your app at it.

1

Start Buggregator

One file, installed with one command. The web UI opens on port 8000. Prefer containers? Use the Docker tab.

$ curl -fsSL https://raw.githubusercontent.com/buggregator/server/master/scripts/install.sh | sh
$ HTTP_ADDR=127.0.0.1:8000 \
  SMTP_ADDR=127.0.0.1:1025 \
  VAR_DUMPER_ADDR=127.0.0.1:9912 \
  MONOLOG_ADDR=127.0.0.1:9913 \
  PROXY_ADDR=127.0.0.1:8080 \
  ~/.local/bin/buggregator

The script picks the file for your CPU: Intel/AMD or ARM.

One file for Linux, macOS and Windows, about 40 MB. Nothing else to install.

This run line keeps it on this computer. Without the *_ADDR settings it listens on all network interfaces

Rather not run a script? Download the file yourself

Runs on Windows, macOS and Linux, on Intel/AMD and ARM.

Prefer not to do it yourself? Your AI agent can set Buggregator up for you