A Laravel package to convert natural language queries into Eloquent queries with AI assistance.
DotKnock/AskDB helps you build natural language interfaces for your Laravel application to query databases securely and dynamically.
By defining your Eloquent models and their metadata (fields, types, descriptions), the package leverages OpenAI to translate user text queries into safe, validated Eloquent query code snippets.
- Define models with field metadata and descriptions for better AI understanding.
- Allow or disallow specific fields to enhance security and control.
- AI-powered natural language parsing into Laravel Eloquent query code.
- Prevents AI from generating queries on unauthorized models or fields.
- Supports field type validation and token-efficient interactions.
- Streamlines building conversational or chatbot-like database query interfaces.
Install via Composer:
composer require dotknock/ask-dbPublish configuration (if applicable):
php artisan vendor:publish --provider="Dotknock\AskDb\AskDbServiceProvider"Set your OpenAI API key in .env:
OPENAI_API_KEY=your_openai_api_key_hereDefine the models you want AI to understand with field names, data types, and descriptions, for example:
use Dotknock\AskDb\Model;
Model::define('Product', [
'fields' => [
'id' => 'integer',
'name' => 'string',
'category' => 'string',
'price' => 'float',
'description' => 'string',
],
'description' => 'Products available in the store, including categories and prices.'
]);You can allow or disallow certain fields for querying:
Model::allow('Product', ['name', 'category', 'price']);
Model::disallow('Product', ['description']);Use the package to convert a user query into Eloquent:
use Dotknock\AskDb\QueryBuilder;
$query = QueryBuilder::fromUserInput('Show me products under 50 dollars in the cosmetics category');
echo $query;Output:
$products = Product::where('price', '<=', 50)
->where('category', 'cosmetics')
->get();- Queries generated only for allowed models and fields.
- Strict filtering to prevent unauthorized data access.
- No direct execution of generated code; you control when and how to run it.
- PHP 8.1+
- Laravel 10.x+
- OpenAI PHP SDK (
openai-php/client) - Internet connection for API calls
Contributions, issues, and feature requests are welcome!
Please open an issue or submit a pull request.
MIT © Saad Majeed
Saad Majeed – saadmajeed.dev@gmail.com
GitHub Repository