PHPackages                             oussemakh1/laravel-query-optimizer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. oussemakh1/laravel-query-optimizer

ActiveLibrary[Database &amp; ORM](/categories/database)

oussemakh1/laravel-query-optimizer
==================================

Enhance Laravel query performance with logging, analysis, and AI-powered insights.

00Blade

Since Jun 14Pushed 11mo agoCompare

[ Source](https://github.com/oussemakh1/laravel-query-optimizer)[ Packagist](https://packagist.org/packages/oussemakh1/laravel-query-optimizer)[ RSS](/packages/oussemakh1-laravel-query-optimizer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Query Optimizer
-----------------------

[](#laravel-query-optimizer)

The **Laravel Query Optimizer** is a package designed to monitor, analyze, and optimize database query performance in Laravel applications. It provides tools to log query execution details, analyze performance metrics, and suggest improvements through a user-friendly dashboard and API endpoints. The package leverages Doctrine DBAL for advanced query analysis and integrates seamlessly with Laravel's service provider architecture.

### Demos

[](#demos)

Here are some demo images showcasing the Laravel Query Optimizer package:

  [![](./assets/14.png)](./assets/14.png) [![](./assets/15.png)](./assets/15.png) ### Features

[](#features)

- **Query Logging**: Records SQL queries, their bindings, execution time, and timestamps.
- **Performance Dashboard**: Visualizes query statistics, execution times, and query distribution.
- **API Endpoints**: Retrieve query metrics and get detailed query explanations using an external AI service (Gemini API).
- **Console Commands**: View and clear query statistics from the command line.
- **Customizable Configuration**: Configure log paths, API keys, and other settings.
- **Doctrine DBAL Integration**: Offers advanced database schema analysis for query optimization.

### Requirements

[](#requirements)

- PHP &gt;= 8.0
- Laravel &gt;= 8.x
- Doctrine DBAL
- Supported database drivers: MySQL, PostgreSQL, SQLite, SQL Server

### Installation

[](#installation)

1. **Install via Composer**:

    ```
    composer require oussemakh1/laravel-query-optimizer
    ```
2. **Publish Configuration**:

    ```
    php artisan vendor:publish --tag=config
    ```

    This will copy the configuration file to `config/queryoptimizer.php`.
3. **(Optional) Publish Views**:

    ```
    php artisan vendor:publish --tag=queryoptimizer-views
    ```

    This will copy dashboard views to `resources/views/vendor/queryoptimizer`.
4. **(Optional) Publish Assets**:

    ```
    php artisan vendor:publish --tag=assets
    ```

    This will copy JavaScript assets to `resources/js/queryoptimizer`.
5. **Configure Gemini API Key** (for query explanations) in `config/queryoptimizer.php`:

    ```
    return [
        'log_path'       => storage_path('logs/query_optimizer.log'),
        'gemini_api_key' => env('GEMINI_API_KEY', ''),
    ];
    ```

    Then add `GEMINI_API_KEY` to your `.env` file.

### Configuration

[](#configuration)

Edit `config/queryoptimizer.php` to customize:

- `log_path`: Path for query logs (default: `storage/logs/query_optimizer.log`).
- `gemini_api_key`: API key for Gemini AI service.
- Other settings as needed.

### Usage

[](#usage)

#### Dashboard

[](#dashboard)

Visit `/query-optimizer/dashboard` to see:

- Total queries executed
- Total &amp; average execution time
- Slowest query details
- Execution time charts
- Query distribution over time
- Detailed logs with filters (all, fast, medium, slow)

#### API Endpoints

[](#api-endpoints)

Under `/api/query-optimizer`:

- **GET** `/metrics` – Returns performance statistics.
- **POST** `/explain` – Analyzes an SQL query and returns an explanation (requires Gemini API key).

Example:

```
curl -X POST http://your-app.com/api/query-optimizer/explain \
  -H "Content-Type: application/json" \
  -d '{"sql": "SELECT * FROM users WHERE created_at > NOW()"}'
```

#### Console Commands

[](#console-commands)

- **Show Stats**:

    ```
    php artisan query-optimizer:stats
    ```
- **Clear Stats**:

    ```
    php artisan query-optimizer:clear-stats
    ```

### How It Works

[](#how-it-works)

1. **Service Provider**: Registers `QueryAnalyzer`, sets up routes, views, and commands.
2. **Query Logging**: `QueryLogger` class logs details to the configured file.
3. **Analysis**: `QueryAnalyzer` tracks queries, manages stats, and integrates Doctrine DBAL for schema insights.
4. **Dashboard**: `DashboardController` renders a Blade view (`dashboard.blade.php`) for metrics.
5. **API**: `ApiController` handles metrics retrieval and query explanations via Gemini.
6. **Console**: `ShowStats` &amp; `ClearStats` commands manage stats from the CLI.

### Customization

[](#customization)

- **Views**: Modify published Blade templates in `resources/views/vendor/queryoptimizer`.
- **JavaScript**: Extend assets in `resources/js/queryoptimizer`.
- **Config**: Adjust `config/queryoptimizer.php` as needed.

### Troubleshooting

[](#troubleshooting)

- **Empty Dashboard**: Check that the `log_path` file is writable and receiving logs.
- **API Errors**: Verify the Gemini API key and network connectivity.
- **DBAL Issues**: Ensure correct database configuration and driver.

### Contributing

[](#contributing)

Contributions are welcome! Please submit a pull request or open an issue on GitHub.

### License

[](#license)

Open-source under the [MIT License](https://opensource.org/licenses/MIT).

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity14

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/e746eb7af5f7bf032249b0ee5b42c9caeec4cdf618c15f0a8435dca10d390409?d=identicon)[oussemakh1](/maintainers/oussemakh1)

---

Top Contributors

[![oussemakh1](https://avatars.githubusercontent.com/u/46871935?v=4)](https://github.com/oussemakh1 "oussemakh1 (5 commits)")

### Embed Badge

![Health badge](/badges/oussemakh1-laravel-query-optimizer/health.svg)

```
[![Health](https://phpackages.com/badges/oussemakh1-laravel-query-optimizer/health.svg)](https://phpackages.com/packages/oussemakh1-laravel-query-optimizer)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
