Query your database using natural language via LLMs. This Laravel app integrates a local package (packages/askmydb/laravel-askmydb) that converts plain-English prompts into safe SQL SELECT statements, executes them against your configured database, and shows the results with optional charts. If you want the package only and not the full working demo, get it here https://github.com/wilocampo/laravel-askmydb
- Inspired by the original Python project: Ask-My-DB (Python)
- Sample data: Chinook database (SQLite): lerocha/chinook-database
- This is a fresh Laravel 12 implementation with OpenAI-compatible and Ollama providers and a simple demo UI.
- Natural language to SQL via LLM
- Safe by default: only SELECTs, with enforced LIMIT
- Database-agnostic (Laravel connections); demo configured for SQLite Chinook
- Schema introspection with limits to stay within LLM context
- Demo UI at
/askmydbwith SQL preview, JSON, and charts (Bar, Line, Area, Pie, Donut, Scatter) - Auto-render charts with axis selectors
- PHP ^8.2, Laravel ^12
- Composer, Node (only if you build your own frontend assets)
composer install
cp .env.example .env # if you don't have one
php artisan key:generate- Place
Chinook_Sqlite.sqlitein the project root (already present). - Configure SQLite in
.envusing a relative path:
DB_CONNECTION=sqlite
DB_DATABASE=Chinook_Sqlite.sqlite- The app resolves relative paths against the project root.
- Clear caches after changes:
php artisan config:clear && php artisan optimize:clearSessions/cache
- For a read-only DB like Chinook, file drivers are used:
SESSION_DRIVER=file
CACHE_DRIVER=file- Local package path:
packages/askmydb/laravel-askmydb - Service provider, routes, controllers, and Blade view are auto-registered via the package.
Set one provider in .env:
ASKMYDB_PROVIDER=openai # or ollama, or dummy
# OpenAI-compatible (OpenAI or OpenRouter)
OPENAI_API_KEY=YOUR_KEY
# For OpenAI: https://api.openai.com/v1
# For OpenRouter: https://openrouter.ai/api/v1
OPENAI_BASE_URL=https://openrouter.ai/api/v1
OPENAI_MODEL=openai/gpt-4o-mini
OPENAI_TEMPERATURE=0.2
# Ollama (local)
OLLAMA_BASE_URL=http://localhost:11434
OLLAMA_MODEL=llama3.1
# Introspection limits (optional)
ASKMYDB_MAX_TABLES=50
ASKMYDB_MAX_COLUMNS=60Notes:
- Temperature must be numeric (e.g.,
0.2). - To use a different DB connection, set
ASKMYDB_CONNECTIONto a Laravel connection name; otherwise it uses the default.
- Visit:
/askmydb - Enter prompts like:
- "List the 10 latest invoices with total and customer name"
- "From Invoice and InvoiceLine, monthly totals for 2024 and 2023"
- The UI shows generated SQL and JSON results, and auto-renders charts.
- Chart types: Bar, Line, Area (line+fill), Pie, Donut, Scatter
- Select X axis and one or more Y axes; chart updates automatically
- Pie/Donut use the first selected Y series; Scatter requires two Y series
- Only README appears after pushing the package repo
- You likely created the repo with an initial README. Do
git push --force-with-lease origin mainfrom the package directory, or pull with--allow-unrelated-historiesthen push.
- You likely created the repo with an initial README. Do
- OpenRouter 400 "Expected number, received string"
- Ensure
OPENAI_TEMPERATUREis numeric; clear config cache.
- Ensure
- Context length exceeded
- Lower
ASKMYDB_MAX_TABLES/ASKMYDB_MAX_COLUMNSor choose a larger-context model.
- Lower
- Model generates invalid SQL (e.g., missing FROM)
- The app sanitizes and falls back to a harmless query. Provide concrete table names from
/askmydb/schema.jsonto guide the model.
- The app sanitizes and falls back to a harmless query. Provide concrete table names from
- Session table missing (SQLite)
- Sessions use
filedriver in.env. Clear cookies and retry if needed.
- Sessions use
- The package sanitizes SQL to allow only SELECT and injects a LIMIT when missing.
- Prefer read-only DB users/replicas for safety.
- Clear caches when tweaking
.env:
php artisan config:clear && php artisan optimize:clear- Quick tinker check:
php artisan tinker --execute="dump(AskMyDB\\Laravel\\Facades\\AskMyDB::ask('list 5 invoices with total and date'));"- MIT for this app and package
- Chinook database licensing per upstream repository
