Interface for interacting with SQL databases using LLM.

Maintainers

Package info

github.com/mirakmalsulton/llmsql

pkg:composer/mirakmalsulton/llmsql

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

dev-main 2026-02-08 16:14 UTC

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