Queries
Flare can collect information about the queries being executed in your application. This includes:
- The query
- The query bindings
- The database name
- The database driver
- The laravel connection
This functionality is enabled by default, but you can disable it by ignoring the Queries collect in config.php:
use Spatie\FlareClient\Enums\CollectType;
'collects' => FlareConfig::defaultCollects(
ignore: [CollectType::Queries],
),
You can configure the maximum number of queries tracked while collecting data in the case of an error as such:
'collects' => FlareConfig::defaultCollects(
extra: [
CollectType::Queries->value => [
'max_items_with_errors' => 10,
],
]
),
Collection of queries will happen automatically, but it is possible to configure the collection.
Finding the origin
Database queries sometimes become a bit tricky to debug. You can find the origin of the query by setting the findOrigin parameter to true:
'collects' => FlareConfig::defaultCollects(
extra: [
CollectType::Queries->value => [
'find_origin' => true,
],
]
),
Now, every query will include the file and line number where the query was executed.
If you only want to find the origins of slow queries, you can pass a findOriginThreshold parameter to the collectQueries method in milliseconds:
use Spatie\FlareClient\Time\TimeHelper;
'collects' => FlareConfig::defaultCollects(
extra: [
CollectType::Queries->value => [
'find_origin_threshold' => TimeHelper::milliseconds(100),
],
]
),
Now, only queries that take longer than 100 milliseconds will include the file and line number where the query was executed. By default, queries slower than 300 milliseconds will be collected.
Be careful not to set this threshold too low, as it can cause a lot of overhead in your application.
Bindings
When you're passing bindings alongside your query, Flare will automatically collect them.
It is possible to disable sending query bindings as such:
'collects' => FlareConfig::defaultCollects(
extra: [
CollectType::Queries->value => [
'include_bindings' => false,
],
]
),
This setting removes bindings from collected query context. It does not redact values already present in SQL or in an exception message, including the query-exception messages described below. Review censoring collected data separately when queries contain sensitive information.
Copying SQL from query exceptions
When Laravel reports a query exception, the Flare Laravel SDK formats its SQL bindings in the exception message. String values are quoted and embedded quotes are escaped, making the SQL easier to copy into a database client.
For example, a message containing an unquoted string value such as where "reference" = order-123 becomes:
select * from "orders" where "reference" = 'order-123'
Open the error occurrence and use the copy button on its SQL snippet. Review the statement before running it, especially if it changes data. Formatting depends on the exception's message and binding shape; it is not a guarantee that every statement can be replayed on every database. A truncated query cannot be recovered by copying the snippet; Flare displays a warning when the query is truncated.
Manually recording queries
If you're executing queries outside of Laravel's database layer, you can record them manually. The PHP documentation provides a full overview of all available recorder methods. When using these methods in Laravel, use the Flare facade instead of $flare:
use Spatie\LaravelFlare\Facades\Flare;
use Spatie\FlareClient\Time\TimeHelper;
Flare::query()->record(
query: 'SELECT * FROM users WHERE id = ?',
duration: TimeHelper::milliseconds(300),
bindings: [1],
databaseName: 'mysql',
driverName: 'mysql',
);