Serialization
dump() turns a tree into a JSON-serializable array and Expression::load() rebuilds it — in the same format as sqlglot's dump()/load(). Useful for caching ASTs, queueing queries, or snapshotting tests.
Dump and load
use Kwasii\LaravelSqlParser\Expressions\Expression;
$json = json_encode($query->dump());
$copy = Expression::load(json_decode($json, true));
$copy->equals($query); // true
The payload is plain arrays, strings, numbers, booleans, and null — safe for json_encode, cache stores, and queues.
Tree format
$tree = $query->toTree();
// ['class' => 'Select', 'args' => [...]]
The Artisan command can print it directly:
php artisan sql:transpile "SELECT 1" --ast
{
"class": "Select",
"args": {
"expressions": [
{ "class": "Literal", "args": { "this": "1", "is_string": false } }
]
}
}
Round-tripping across dialects
A dumped tree is dialect-agnostic. Load it anywhere and generate for any dialect:
$copy = Expression::load(json_decode($json, true));
$copy->sql('mysql');
$copy->sql('postgres');
$copy->sql('sqlite');
Equality
$a = Sql::parseOne('SELECT a FROM t', 'mysql');
$b = Sql::parseOne('select a from t', 'mysql');
$a->equals($b); // true — structural comparison, not string comparison