PHPackages                             eloquent-dsl/ai-query - 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. eloquent-dsl/ai-query

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

eloquent-dsl/ai-query
=====================

Safe DSL-driven Eloquent query builder for AI agents.

12↓100%PHP

Since May 5Pushed 1mo agoCompare

[ Source](https://github.com/VictorDevPHP/ai-query-laravel)[ Packagist](https://packagist.org/packages/eloquent-dsl/ai-query)[ RSS](/packages/eloquent-dsl-ai-query/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Eloquent DSL AI Query
=====================

[](#eloquent-dsl-ai-query)

 [![Laravel Logo](https://camo.githubusercontent.com/add54281c9fb74f167b2c1f1e13468346d6ef8885dc90eb7036e09b579462947/68747470733a2f2f6c61726176656c2e636f6d2f696d672f6c6f676f6d61726b2e6d696e2e737667)](https://camo.githubusercontent.com/add54281c9fb74f167b2c1f1e13468346d6ef8885dc90eb7036e09b579462947/68747470733a2f2f6c61726176656c2e636f6d2f696d672f6c6f676f6d61726b2e6d696e2e737667)

[![Latest Stable Version](https://camo.githubusercontent.com/e6510446480f2599fda3a6517e76b82d5bd7976254188303552ea1a66ada7f30/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f656c6f7175656e742d64736c2f61692d7175657279)](https://packagist.org/packages/eloquent-dsl/ai-query)[![Total Downloads](https://camo.githubusercontent.com/21121b7e5f35287b07c0b122a7c5ef1b56087907789b598fa8e6bef348ea287b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f656c6f7175656e742d64736c2f61692d7175657279)](https://packagist.org/packages/eloquent-dsl/ai-query)[![License](https://camo.githubusercontent.com/f96d5f7b6ff526f1e110b9e8a34ff293762dc1886220bd2756fa16a679cffe1a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f656c6f7175656e742d64736c2f61692d7175657279)](https://packagist.org/packages/eloquent-dsl/ai-query)[![PHP Version](https://camo.githubusercontent.com/7a3254af0b3d6fc989bf47842f1bafe36e7b5467088a7e2f721bda8ae9cc0816/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f656c6f7175656e742d64736c2f61692d7175657279)](https://packagist.org/packages/eloquent-dsl/ai-query)

About Eloquent DSL AI Query
---------------------------

[](#about-eloquent-dsl-ai-query)

`eloquent-dsl/ai-query` is a reusable Laravel package that allows AI agents to build safe, structured, and dynamic Eloquent queries from a DSL-like payload.

Instead of generating raw SQL or executing arbitrary code, AI clients can send constrained actions that are validated and executed with strict allow/block lists.

Features
--------

[](#features)

- Fluent API: `AIQuery::for(Model::class)->fromArray($input)->get();`
- Structured actions (`select`, `where`, `orWhere`, `whereIn`, `whereBetween`, `join`, `with`, `whereHas`, `orderBy`, `limit`, `paginate`)
- Runtime-extensible action registry
- Safe join mapping (no dynamic ON conditions from AI)
- Model-level security rules (allow list + block list)
- Query complexity control (`max_actions`)
- Optional logging for executed AI queries
- Model introspection service

Requirements
------------

[](#requirements)

- PHP `^7.4 || ^8.0`
- Laravel `7+`

Installation
------------

[](#installation)

### 1) Add package as path repository (without Packagist)

[](#1-add-package-as-path-repository-without-packagist)

In your Laravel app `composer.json`:

```
{
  "repositories": [
    {
      "type": "path",
      "url": "../eloquent-dsl"
    }
  ],
  "require": {
    "eloquent-dsl/ai-query": "*"
  }
}
```

Then run:

```
composer update eloquent-dsl/ai-query
```

### 2) Publish config (optional)

[](#2-publish-config-optional)

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

Configuration
-------------

[](#configuration)

`config/ai-query.php`:

```
return [
    'max_limit' => 100,
    'max_actions' => 20,

    'logging' => [
        'enabled' => true,
        'channel' => null,
    ],

    'models' => [
        App\Models\User::class => [
            'allowed_fields' => ['id', 'name', 'email', 'created_at'],
            'blocked_fields' => [],
            'allowed_relations' => ['posts'],
            'blocked_relations' => [],
            'allowed_joins' => ['posts'],
            'joins' => [
                'posts' => [
                    'table' => 'posts',
                    'first' => 'users.id',
                    'operator' => '=',
                    'second' => 'posts.user_id',
                    'type' => 'inner',
                ],
            ],
        ],
    ],
];
```

Basic Usage
-----------

[](#basic-usage)

```
use App\Models\User;
use EloquentDsl\AIQuery\AIQuery;

$input = [
    'model' => User::class,
    'actions' => [
        ['type' => 'select', 'fields' => ['id', 'name', 'email']],
        ['type' => 'where', 'field' => 'email', 'operator' => 'like', 'value' => '%gmail%'],
        ['type' => 'with', 'relation' => 'posts'],
        ['type' => 'join', 'table' => 'posts'],
        ['type' => 'orderBy', 'field' => 'created_at', 'direction' => 'desc'],
        ['type' => 'limit', 'value' => 10],
    ],
];

$users = AIQuery::for(User::class)
    ->fromArray($input)
    ->get();
```

Registering Custom Actions
--------------------------

[](#registering-custom-actions)

```
use EloquentDsl\AIQuery\ActionRegistry;

ActionRegistry::register('customAction', function ($query, array $action): void {
    $query->whereNotNull($action['field'] ?? 'id');
});
```

Runtime Config Override
-----------------------

[](#runtime-config-override)

```
AIQuery::for(User::class)
    ->overrideConfig([
        'max_limit' => 50,
    ])
    ->fromArray($input)
    ->get();
```

Model Introspection
-------------------

[](#model-introspection)

```
use EloquentDsl\AIQuery\ModelInspector;

$all = ModelInspector::getModelsStructure('*');
$specific = ModelInspector::getModelsStructure([
    App\Models\User::class,
]);
```

Example return:

```
[
    'User' => [
        'table' => 'users',
        'fields' => ['id', 'name', 'email'],
        'relations' => ['posts'],
    ],
]
```

Supported Actions
-----------------

[](#supported-actions)

- `select`
- `where`
- `orWhere`
- `whereIn`
- `whereBetween`
- `join` (safe mapped joins only)
- `with`
- `whereHas`
- `orderBy`
- `limit`
- `paginate`

Testing
-------

[](#testing)

Run package tests locally:

```
composer install
vendor/bin/phpunit
```

Security
--------

[](#security)

This package is designed for constrained query generation, but secure behavior depends on proper configuration.

Always configure:

- `allowed_fields`
- `allowed_relations`
- `allowed_joins`
- sensible `max_limit` and `max_actions`

Never expose unrestricted model access to AI inputs.

Contributing
------------

[](#contributing)

Thank you for considering contributing to this package.

License
-------

[](#license)

This package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance61

Regular maintenance activity

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity11

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/30ce88fb027e27d79503dd8d864fe8d8e5cd74357c3ab0b2979e05384fd9f01c?d=identicon)[victorsmp](/maintainers/victorsmp)

---

Top Contributors

[![VictorDevPHP](https://avatars.githubusercontent.com/u/98700678?v=4)](https://github.com/VictorDevPHP "VictorDevPHP (2 commits)")

### Embed Badge

![Health badge](/badges/eloquent-dsl-ai-query/health.svg)

```
[![Health](https://phpackages.com/badges/eloquent-dsl-ai-query/health.svg)](https://phpackages.com/packages/eloquent-dsl-ai-query)
```

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k116.5M113](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8361.6M87](/packages/propel-propel1)[mpociot/laravel-composite-key

Support composite keys in your laravel app.

3544.8k1](/packages/mpociot-laravel-composite-key)

PHPackages © 2026

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