socialblue/laravel-query-adviser
Installation:
composer require socialblue/laravel-query-adviser
Publish the config file:
php artisan vendor:publish --provider="Socialblue\QueryAdviser\QueryAdviserServiceProvider" --tag="config"
Enable Logging:
Add the middleware to your app/Http/Kernel.php:
protected $middleware = [
// ...
\Socialblue\QueryAdviser\Middleware\LogQueries::class,
];
First Use Case:
Visit /query-adviser in your browser. The dashboard will display all logged queries during your session, sorted by execution time (descending). Click any query to:
Debugging Slow Endpoints:
/query-adviser to inspect queries.Execution Time to identify bottlenecks.Performance Profiling:
WHERE clauses or JOINs to spot N+1 or over-fetching.EXPLAIN plans.Integration with Existing Tools:
'enabled' => env('APP_ENV') !== 'production',
if ($query->duration > 500) {
Sentry::captureException(new \Exception("Slow query: {$query->sql}"));
}
CI/CD Pipeline:
php artisan query-adviser:check --threshold=100
Conditional Logging:
Disable logging for specific routes/models in config/query-adviser.php:
'ignore' => [
'routes' => ['admin.*'],
'models' => ['App\Models\CacheableModel'],
],
Custom Query Analysis:
Extend the Query model to add metadata:
// app/Providers/QueryAdviserServiceProvider.php
QueryAdviser::extend(function ($query) {
$query->setAttribute('user_id', auth()->id());
});
Export Data:
Use the query-adviser:export command to generate CSV/JSON reports:
php artisan query-adviser:export --days=7 --to=queries.csv
Performance Overhead:
'enabled' => env('APP_ENV') !== 'production',
cache driver for storage to reduce I/O:
'driver' => 'cache',
Memory Leaks:
php artisan query-adviser:clear
Binding Data Exposure:
app/Providers/QueryAdviserServiceProvider.php:
QueryAdviser::filterBindings(function ($bindings) {
return array_map(fn($val) => str_starts_with($val, '****'), $bindings);
});
Route Caching Conflicts:
php artisan route:cache, ensure middleware is registered after caching:
// Kernel.php
protected $middlewareGroups = [
'web' => [
// ...
\Socialblue\QueryAdviser\Middleware\LogQueries::class,
],
];
Query Not Showing?
Kernel.php).APP_DEBUG=true in .env (some drivers require debug mode).Slow Queries Missing?
config/query-adviser.php for ignored routes/models.QueryAdviser::forceLog() in a service provider to force-log specific queries:
QueryAdviser::forceLog();
User::query()->where(...)->get();
Rerun Fails:
.env.Storage Drivers:
Custom Query Cards:
Override the Blade view (resources/views/vendor/query-adviser/card.blade.php) to add:
JOIN count).email column").Webhook Alerts: Trigger alerts for slow queries via a listener:
// app/Providers/QueryAdviserServiceProvider.php
QueryAdviser::listen(function ($query) {
if ($query->duration > 1000) {
Http::post('https://your-monitoring-service', [
'query' => $query->sql,
'duration' => $query->duration,
]);
}
});
Query Grouping:
Extend the groupBy functionality to categorize by:
QueryAdviser::extendGrouping(function ($query) {
return auth()->check() ? auth()->user()->role : 'guest';
});
How can I help you explore Laravel packages today?