doctrine/sql-formatter
Formats SQL queries into readable, consistently indented output. Helps debug logs, review generated SQL, and improve diffs by standardizing whitespace and keywords. Supports multiple SQL dialect features and runs as a lightweight PHP library.
Install via Composer: composer require doctrine/sql-formatter. Start by using the SqlFormatter class: instantiate it and call format() with a raw SQL string. For debugging, simply wrap query output in logs or exceptions:
use Doctrine\SqlFormatter\SqlFormatter;
$formatter = new SqlFormatter();
$formatted = $formatter->format('SELECT * FROM users WHERE id = 1');
echo $formatted;
//多行缩进、关键词高亮的美化输出
First use case: injecting formatted SQL into Laravel’s query log (DB::enableQueryLog()) or exception handling to make slow/failing queries instantly readable.
laravel.log or external services (e.g., via a logger decorator).expect($sql)->toBe($expectedFormatted)); stable output prevents flaky diffs when SQL order/spacing changes but semantics stay identical.$sql = SqlFormatter::format($query);
$this->info($sql);
/* */) can干扰 parsing—avoid inline comments near edge cases.SqlFormatter::format() options for stripComments, compress, or highlight flags—e.g., disable color codes in CLI loggers using highlight: false.SqlFormatter to customize keyword casing, indent size, or line length—but be cautious; minor overrides can break stability guarantees for diff-based tests.How can I help you explore Laravel packages today?