sarfraznawaz2005/aiqueryoptimizer
Installation
composer require sarfraznawaz2005/aiqueryoptimizer
php artisan vendor:publish --provider="Sarfraznawaz2005\AIQueryOptimizer\AIQueryOptimizerServiceProvider"
Configuration
.env:
AIQUERY_OPTIMIZER_PROVIDER=gemini
AIQUERY_OPTIMIZER_GEMINI_API_KEY=your_gemini_key_here
config/aiqueryoptimizer.php:
'enabled' => env('AIQUERY_OPTIMIZER_ENABLED', true),
First Use Case
resources/views/layouts/app.blade.php):
@aiqueryoptimizer
Query Capture & Analysis
SELECT queries. No manual instrumentation needed.max_queries_to_analyze).Integration with Existing Tools
'debugbar' => [
'enabled' => false,
],
'ajax_support' => true,
Conditional Analysis
'environments' => ['local', 'staging'],
'ignored_routes' => ['api/*'],
Custom AI Prompts
config/aiqueryoptimizer.php:
'ai_prompt_template' => "Analyze this SQL query: {query}. Provide optimization suggestions tailored to this schema: {schema}. Focus on: {focus_areas}.",
focus_areas (e.g., "indexes", "joins") via middleware:
public function handle($request, Closure $next)
{
config(['aiqueryoptimizer.focus_areas' => ['indexes']]);
return $next($request);
}
Batch Processing
aiquery_optimizer_queries table:
use Sarfraznawaz2005\AIQueryOptimizer\Facades\AIQueryOptimizer;
$results = AIQueryOptimizer::analyzeBatch(
now()->subHours(1),
now(),
50 // limit
);
API Rate Limits
'cache' => [
'enabled' => true,
'ttl' => 3600, // 1 hour
],
aiquery_optimizer_ai_calls table.Schema Context Gaps
schema table in your database. Ensure it’s up-to-date:
php artisan aiqueryoptimizer:refresh-schema
'schema_context_limit' => 5000, // characters
AJAX Query Dropping
ajax_support is enabled in config.aiqueryoptimizer middleware is registered in App\Http\Kernel.php:
'web' => [
\Sarfraznawaz2005\AIQueryOptimizer\Http\Middleware\CaptureQueries::class,
],
False Positives in Suggestions
$analysis = AIQueryOptimizer::analyze($query);
$suggestions = collect($analysis['suggestions'])
->where('execution_count', '>', 10); // Only act on queries run >10 times
Performance Overhead
'environments' => ['local'],
Log Queries Manually
use Sarfraznawaz2005\AIQueryOptimizer\Facades\AIQueryOptimizer;
AIQueryOptimizer::logQuery('SELECT * FROM users WHERE id = ?', [1]);
Inspect Raw AI Input/Output
'debug' => [
'enabled' => true,
'log_path' => storage_path('logs/aiquery_optimizer.log'),
],
Clear Cache
php artisan cache:clear
php artisan aiqueryoptimizer:clear-cache
Custom AI Providers
Sarfraznawaz2005\AIQueryOptimizer\Contracts\AIProvider:
namespace App\Providers;
use Sarfraznawaz2005\AIQueryOptimizer\Contracts\AIProvider;
class CustomAIProvider implements AIProvider {
public function analyze(string $query, array $context): string {
// Your custom logic here
}
}
config/aiqueryoptimizer.php:
'providers' => [
'custom' => \App\Providers\CustomAIProvider::class,
],
Query Filtering
aiqueryoptimizer.capture event:
use Sarfraznawaz2005\AIQueryOptimizer\Events\QueryCaptured;
QueryCaptured::listen(function (QueryCaptured $event) {
if (str_contains($event->query, 'temp_')) {
$event->skip = true; // Skip temporary table queries
}
});
UI Customization
resources/views/vendor/aiqueryoptimizer/modal.blade.php) to:
Post-Analysis Actions
aiqueryoptimizer.analyzed event to automate optimizations:
use Sarfraznawaz2005\AIQueryOptimizer\Events\QueryAnalyzed;
QueryAnalyzed::listen(function (QueryAnalyzed $event) {
if ($event->suggestions->contains('add_index', 'users_email')) {
Artisan::call('make:index', [
'table' => 'users',
'column' => 'email',
'--unique' => true,
]);
}
});
How can I help you explore Laravel packages today?