PHPackages                             dotknock/ask-db - 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. dotknock/ask-db

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

dotknock/ask-db
===============

This package will help communicating with Database based on User Prompts with the help of AI

20PHP

Since May 28Pushed 1y ago1 watchersCompare

[ Source](https://github.com/SaadMajeed565/AskDB)[ Packagist](https://packagist.org/packages/dotknock/ask-db)[ RSS](/packages/dotknock-ask-db/feed)WikiDiscussions main Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

DotKnock/AskDB
==============

[](#dotknockaskdb)

**A Laravel package to convert natural language queries into Eloquent queries with AI assistance.**

---

Overview
--------

[](#overview)

`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.

---

Features
--------

[](#features)

- 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.

---

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

[](#installation)

Install via Composer:

```
composer require dotknock/ask-db
```

Publish 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_here
```

---

Usage
-----

[](#usage)

### Step 1: Define your models and fields

[](#step-1-define-your-models-and-fields)

Define 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.'
]);
```

### Step 2: Configure field permissions

[](#step-2-configure-field-permissions)

You can allow or disallow certain fields for querying:

```
Model::allow('Product', ['name', 'category', 'price']);
Model::disallow('Product', ['description']);
```

### Step 3: Process user natural language query

[](#step-3-process-user-natural-language-query)

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', '
