eloquent-dsl/ai-query
Composer 安装命令:
composer require eloquent-dsl/ai-query
包简介
Safe DSL-driven Eloquent query builder for AI agents.
README 文档
README
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
- 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
- PHP
^7.4 || ^8.0 - Laravel
7+
Installation
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)
php artisan vendor:publish --tag=ai-query-config
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
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
use EloquentDsl\AIQuery\ActionRegistry; ActionRegistry::register('customAction', function ($query, array $action): void { $query->whereNotNull($action['field'] ?? 'id'); });
Runtime Config Override
AIQuery::for(User::class) ->overrideConfig([ 'max_limit' => 50, ]) ->fromArray($input) ->get();
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
selectwhereorWherewhereInwhereBetweenjoin(safe mapped joins only)withwhereHasorderBylimitpaginate
Testing
Run package tests locally:
composer install vendor/bin/phpunit
Security
This package is designed for constrained query generation, but secure behavior depends on proper configuration.
Always configure:
allowed_fieldsallowed_relationsallowed_joins- sensible
max_limitandmax_actions
Never expose unrestricted model access to AI inputs.
Contributing
Thank you for considering contributing to this package.
License
This package is open-sourced software licensed under the MIT license.
eloquent-dsl/ai-query 适用场景与选型建议
eloquent-dsl/ai-query 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 05 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 eloquent-dsl/ai-query 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 eloquent-dsl/ai-query 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 45
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-05-04