Introduction
Laravel SQL Parser is a fast SQL parser and transpiler for Laravel, written in pure PHP. It parses SQL into a syntax tree and writes it back out in another dialect: MySQL to SQL Server, Postgres to SQLite, and so on.
It is a port of sqlglot (v30.21.0) and passes sqlglot's own test cases for every supported dialect.
What it does
- Transpile SQL between 40+ dialects with one call
- Parse SQL into an expression tree you can inspect, rewrite, and regenerate
- Build queries fluently with a
Builder API
- Optimize queries with a full optimizer (qualify, simplify, pushdown, merge, unnest)
- Integrate with Laravel via a facade, query-builder macros, config, cache, and an Artisan command
- Run anywhere: the parser core has zero Laravel dependencies, so plain PHP scripts work too
Why use it
- You support multiple database drivers and need one codebase to emit correct SQL per driver
- You migrate between databases (e.g. MySQL to Postgres) and need deterministic rewrites
- You analyze SQL: list tables and columns, validate syntax, expand stars, rewrite placeholders
- You want sqlglot-grade dialect coverage without leaving PHP
How it works
use Kwasii\LaravelSqlParser\Facades\LaravelSqlParser as Sql;
Sql::transpile('SELECT IFNULL(a, 1) FROM t LIMIT 5', from: 'mysql', to: 'tsql');
// ['SELECT TOP 5 COALESCE(a, 1) FROM t']
- The tokenizer turns SQL text into tokens for the source dialect.
- The parser builds an expression tree (
Select, Column, Table, functions, etc.).
- The generator writes the tree back out for the target dialect, applying rewrites (
IFNULL to COALESCE, LIMIT to TOP, quoting style, placeholders, and more).
Everything else — query-builder macros, the optimizer, the builder, the Artisan command — is built on this parse/generate core.
Next steps