mirakmalsulton / llmsql
Interface for interacting with SQL databases using LLM.
dev-main
2026-02-08 16:14 UTC
Requires
- php: >=7.4
- ext-json: *
- guzzlehttp/guzzle: ^7.0
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2026-04-08 16:28:47 UTC
README
Below is an example of how to implement a natural language SQL interface within a Laravel route and view.
1. Route and Controller Logic
Add this to your routes/web.php. It automatically fetches your database schema, processes the question via the engine, and executes the generated SQL.
use Illuminate\Http\Request; use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\DB; use Mirakmalsulton\LlmSql\LlmSqlEngine; use Mirakmalsulton\LlmSql\Dto\Tables; Route::match(['get', 'post'], '/sql', function (Request $request, LlmSqlEngine $engine) { $question = $request->input('question'); $result = null; $rows = collect(); $headers = []; if ($request->isMethod('post') && $question) { $allTables = Schema::getTableListing(); $tables = new Tables(); foreach ($allTables as $table) { $ddl = (array)DB::select("SHOW CREATE TABLE `{$table}`")[0]; $columns = Schema::getColumnListing($table); $tables->addTable($table, $columns, $ddl['Create Table']); } try { $result = $engine->setTables($tables)->find($question); $dbData = DB::select($result['sql']); $rows = collect($dbData)->map(fn($item) => (array)$item); $headers = $rows->first() ? array_keys($rows->first()) : []; } catch (\Exception $e) { $headers = ['Error']; $rows = collect([['error' => $e->getMessage()]]); } } return view('welcome', [ 'result' => $result, 'rows' => $rows, 'headers' => $headers, 'question' => $question ]); })->name('sql');
2. View (Blade)
Use the built-in widgets to render the search interface and the results table in your welcome.blade.php.
{{-- Render the SQL search widget --}} {!! \Mirakmalsulton\LlmSql\Widgets\SimpleSqlWidget::render(route('sql'), $result, $question) !!} {{-- Render results table if query was successful --}} @if($result && isset($rows)) <div style="margin-top: 30px;"> <h4 style="font-family: sans-serif;">Results:</h4> {!! \Mirakmalsulton\LlmSql\Widgets\SimpleSqlWidget::renderTable($headers, $rows->toArray()) !!} </div> @endif